gpt4 book ai didi

ios - Linkedin 登录使用 oAuth 1.0a

转载 作者:行者123 更新时间:2023-11-28 15:09:51 25 4
gpt4 key购买 nike

我正在快速使用 oAuth 2.0 开发 linkedin Api。主要问题是获取到access_token后没有在UIWebView中写入cookie。我试过这个`

oginWebView.loadRequest(URLRequest(url: URL(string : "https://www.instagram.com/")!))

获取access_token 后显示登录页面而不是/feed 页面。我的应用程序需要 cookie。我检查了很多帖子,但没有在 oAuth 1.0a 中获得任何适当的身份验证解决方案。我正在使用 Instagram oauth,它还会给我 cookie。我认为这是由于 oAuth 1.0。我对 oAuth 了解不多。

任何人都可以给我简单的登录示例(swift,objective-c)在 webview 中使用 oAuth 1.0a 进行 linkedin 身份验证。或任何其他保存 cookie 的解决方案。或者我如何将下面的代码用于 linkedin。谢谢。
oAuth 1.0 Sample

struct INSTAGRAM_IDS {

static let INSTAGRAM_AUTHURL = "https://api.instagram.com/oauth/authorize/"
static let INSTAGRAM_APIURl = "https://api.instagram.com/v1/users/"
static let INSTAGRAM_CLIENT_ID = ""
static let INSTAGRAM_CLIENTSERCRET = ""
static let INSTAGRAM_REDIRECT_URI = ""
static let INSTAGRAM_ACCESS_TOKEN = "access_token"
static let INSTAGRAM_SCOPE = "likes+comments+relationships+public_content+follower_list"
}

func unSignedRequest () {
let authURL = String(format: "%@?client_id=%@&redirect_uri=%@&response_type=token&scope=%@&DEBUG=True", arguments: [INSTAGRAM_IDS.INSTAGRAM_AUTHURL,INSTAGRAM_IDS.INSTAGRAM_CLIENT_ID,INSTAGRAM_IDS.INSTAGRAM_REDIRECT_URI, INSTAGRAM_IDS.INSTAGRAM_SCOPE ])
let urlRequest = URLRequest.init(url: URL.init(string: authURL)!)
loginWebView.loadRequest(urlRequest)
}

func checkRequestForCallbackURL(request: URLRequest) -> Bool {

let requestURLString = (request.url?.absoluteString)! as String

if requestURLString.hasPrefix(INSTAGRAM_IDS.INSTAGRAM_REDIRECT_URI) {
print(requestURLString, "RedirectURL")
let range: Range<String.Index> = requestURLString.range(of: "#access_token=")!
handleAuth(authToken: requestURLString.substring(from: range.upperBound))
self.dismiss(animated: true, completion: nil)
return false;
}
return true
}

func handleAuth(authToken: String) {
print("Instagram authentication token ==", authToken)
downloadImage(url: URL(string: "https://api.instagram.com/v1/users/self?access_token=\(authToken)")!)
}

func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
return checkRequestForCallbackURL(request: request)
}

最佳答案

你可以试试下面的代码:

import Foundation

let headers = [
"cache-control": "no-cache",
]

let request = NSMutableURLRequest(url: NSURL(string: "https://www.linkedin.com/oauth/v2/authorization?response_type=code&client_id=123456789&redirect_uri=https%3A%2F%2Fwww.example.com%2Fauth%2Flinkedin&state=987654321&scope=r_basicprofile")! as URL,
cachePolicy: .useProtocolCachePolicy,
timeoutInterval: 10.0)
request.httpMethod = "GET"
request.allHTTPHeaderFields = headers

let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
if (error != nil) {
print(error)
} else {
let httpResponse = response as? HTTPURLResponse
print(httpResponse)
}
})

dataTask.resume()

请务必更改 NSURL 中的查询字符串参数。响应 header 将返回其中包含 cookie 的数据。

关于ios - Linkedin 登录使用 oAuth 1.0a,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47859593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com