簡體   English   中英

使用oAuth 1.0a的Linkedin登錄

[英]Linkedin Login Using oAuth 1.0a

我正在使用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),使用oAuth 1.0a在Webview中進行身份驗證。 或任何其他保存cookie的解決方案。 或者我如何使用下面的代碼為linkedin。 謝謝 。
oAuth 1.0示例

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的querystring參數。 響應頭將返回其中包含cookie的數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM