2016-05-03 76 views
3

我能夠使用LinkedIn SDK快速獲取LinkedIn LinkedIn帳戶的訪問令牌。如何使用Stormpath驗證此登錄信息?LinkedIn登錄Stormpath

[更新]

設APIURL = 「https://api.stormpath.com/v1/applications/LI_APPLICATION_ID /帳戶」

func sendRequestWithJSON(accessToken:String) 
{ 
    let json = [ "providerData" : ["providerId": "linkedin", "accessToken": accessToken] ] 


    do { 

     let jsonData = try NSJSONSerialization.dataWithJSONObject(json, options: .PrettyPrinted) 

     let username = STORMPATH_API_KEY_ID 
     let password = STORMPATH_API_KEY_SECRET 
     let loginString = NSString(format: "%@:%@", username, password) 
     let loginData: NSData = loginString.dataUsingEncoding(NSUTF8StringEncoding)! 
     let base64LoginString = loginData.base64EncodedDataWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength) 

     // create post request 
     let url = NSURL(string: APIURL)! 
     let request = NSMutableURLRequest(URL: url) 
     request.HTTPMethod = "POST" 


     // insert json data to the request 
     request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization") 
     request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type") 
     request.HTTPBody = jsonData 


     let task = NSURLSession.sharedSession().dataTaskWithRequest(request){ data, response, error in 
      if error != nil{ 
       print("Error -> \(error)") 
       return 
      } 

      do { 
       let result = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String:AnyObject] 

       print("Result -> \(result)") 

      } catch { 
       print("Error -> \(error)") 
      } 
     } 

     task.resume() 
     //return task 
    } catch { 
     print(error) 
    } 
} 

我傳遞的accessToken從LinkedIn到上述功能取得,但它返回以下結果:

[「message」:需要認證,「status」:401,「code」:401,「developerMessage」:A必須使用有效的API密鑰進行驗證。「moreInfo」:http://www.stormpath.com/docs/quickstart/connect]

我在做什麼?

+1

[iOS linkedin認證]的可能重複(http://stackoverflow.com/questions/28491280/ios-linkedin-authentication) – JAL

+1

我不認爲它是重複的。鏈接的問題是詢問關於vanilla LinkedIn登錄,而這個問題是關於Stormpath支持的應用程序。 –

回答

5

LinkedIn是一個有趣的東西,因爲它們的移動SDK有兩個缺陷:

所以 - 獲得身份驗證的移動工作,我會建議使用一個服務器來處理流量,所以你不必擔心這兩個缺點。這大致是:

  1. 該應用程序會將用戶重定向到您的網絡服務器。
  2. 網絡服務器開始LinkedIn認證流程,並將用戶重定向到LinkedIn。
  3. 用戶登錄LinkedIn,並被重定向回您的網絡服務器。
  4. 網絡服務器讀取響應,並與LinkedIn交換授權碼以獲取訪問令牌。
  5. 網絡服務器將您的用戶重定向迴應用程序,使用自定義網址方案向其發送LinkedIn訪問令牌。
  6. 該應用程序使用LinkedIn訪問令牌登錄到Stormpath。

聽起來很複雜嗎?這實際上比看起來更直接。如果你想嘗試一下,我其實寫了一些demo code for this flow using Express.js & Swift。請讓我知道這對你有沒有用!

+0

謝謝愛德華的幫助。 讓我試試你提供的例子(將接受它作爲回答一次;)) –

+0

嗨,愛德華,你能告訴我什麼設置我需要做與迅速項目來運行你的應用程序?我無法在模擬器上運行此應用程序。 –

+0

你有什麼錯誤?它應該開箱即用。 –