2017-04-06 216 views
0

我使用OAuth構建項目swift,我已經從我的API登錄獲取了Acces令牌,然後我想從我的acces令牌獲取API JSon的數據。因此,如何使用OAuth獲得請求的HTTP方法通過OAuth和我的存取權限令牌如何發送https GET請求與oauth

我有JSON解析STANDAR沒有OAuth的

let url = NSURL(string: "https://conversation.8villages.com/1.0/contents/articles?state=published") 
    let request = NSMutableURLRequest(url: url! as URL) 
    request.httpMethod = "GET" 

    request.addValue("application/json", forHTTPHeaderField: "Authorization") 

    let task = URLSession.shared.dataTask(with: request as URLRequest) { data,response,error in 

     guard error == nil && data != nil else { 
      print("error", error!) 
      return 
     } 

     let httpStatus = response as? HTTPURLResponse 

     if httpStatus!.statusCode == 200 
     { 
      if data?.count != 0 
      { 
       let responString = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) 
       print(responString) 
      } 
      else{ 
       print("No got data from URL") 
      } 
     } 
     else 
     { 
      print("error httpstatus code is ", httpStatus!.statusCode) 
     } 
    } 

    task.resume() 

我已經習慣Librabry https://github.com/OAuthSwift/OAuthSwift

在那裏我得到簽名的請求(Readme.md)這樣

oauthswift.client.get("https://api.linkedin.com/v1/people/~", 
success: { response in 
let dataString = response.string 
print(dataString) 
}, 
failure: { error in 
print(error) 
} 
) 

,但即時通訊混淆如何添加我的艾策斯令牌,使用者密鑰和我的消費者的祕密進入電影我的API JSON

回答

0

如果您有訪問令牌,看起來您已經使用了您的使用者密鑰和使用者密碼,並且您已通過身份驗證。您需要在API獲取調用的授權標頭中傳遞載體訪問令牌。

+0

如何考績承載令牌先生?請告訴我:( –

0

我不熟悉OAuthSwift框架,但我猜的錯誤是以下行:

request.addValue("application/json", forHTTPHeaderField: "Authorization") 

也許應該是:

request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
request.addValue(tokenType + " " + accessToken, forHTTPHeaderField: "Authorization") 

tokenType了OAuth的授權碼補助鍵入始終是字符串BeareraccessToken當然是您從早期獲得的訪問令牌。

編輯:與微軟Azure AD 比較:

GET /v1.0/me/messages 
Host: https://graph.microsoft.com 
Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUz... 
+0

先生,我的accesToken是2個關鍵,如令牌密鑰和令牌祕密。所以。如何添加它們? –