使用oAuth 1.0a登录Linkedin

问题描述 投票:0回答:1

我正在使用oAuth 2.0在swift中使用linkedin Api。主要的问题是它在获得UIWebView后没有在access_token写饼干。我试过这个`

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

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

任何人都可以使用oAuth 1.0a在webview中为我提供简单的登录示例(swift,objective-c)。或任何其他保存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)
    }
ios objective-c swift oauth linkedin
1个回答
1
投票

您可以尝试以下代码:

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中的查询字符串参数。响应标头将返回其中的cookie数据。

© www.soinside.com 2019 - 2024. All rights reserved.