ios - Linkedin 登录使用 oAuth 1.0a

标签 ios objective-c swift oauth linkedin

我正在快速使用 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/

相关文章:

ios - 找不到此可执行文件的有效配置文件错误 Xcode 9

iphone - 如何获取相册中图片的文件名和标签?

ios - CoreImage CIHueAdjust 滤镜给出错误的颜色

swift : Why is the class method struck out

objective-c - 在 Swift 中,您将如何编写 scrollViewWillEndDragging withVelocity targetContentOffset 方法?

ios - 代码签名配置文件 : What do I need to select? 我需要运行什么方案才能发布?

ios - 无法通过 nsdictionary 中的键访问值

objective-c - UINavigationController 样式

ios - 没有正确获得合理的 UILabel 属性文本

swift - 将字符串索引转换为 Int