2016-08-18 79 views
1

我想做一個Unsplash照片客戶端,它需要我使用網站的API。根據文檔,爲了訪問照片相關信息(如pageURL,imagesURL等)oAuth2.0是必需的。它聲明我必須做以下事情:我應該如何在swift中獲得OAuth2.0訪問令牌?

  1. 指示用戶帶有一些查詢參數到https://unsplash.com/oauth/authorize
  2. 如果用戶接受該請求,則用戶將被重定向到redirect_ui(的參數之一),與在代碼查詢參數
  3. 授權碼使POST請求https://unsplash.com/oauth/token一些參數。

這是我試過的代碼來實現這一點:

ViewController.swift

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    authenticator() 
    // Do any additional setup after loading the view, typically from a nib. 
} 


func authenticator(){ 

    Alamofire.request(.GET, "https://unsplash.com/oauth/authorize", parameters: [ 
    "client_id": "2902affb00634248f77b3c5c7a4ba4232fa36e3cbaad826b223a27f3df57e642" 
    , "redirect_uri": "Sample//:" , 
     "response_type" : "code" , 
     "scope":"" ]).responseJSON{ response in 
print("original URL request : \(response.request) ") 
print("URL response : \(response.response) ") 
    } 


    Alamofire.request(.POST, "https://unsplash.com/oauth/authorize", parameters: [ 
     "client_id" : "2902affb00634248f77b3c5c7a4ba4232fa36e3cbaad826b223a27f3df57e642", 
     "client_secret":"6e85c0e5bcda025f0553e1343dcbdd8a892b0db1cc3a653055d84ce44c217bb9", 
     "redirect_uri" : "Sample//:" , 
     "code" : "c15781809f4ee781e6019b958a2702be0dab61e89df96863693dbd7d48bacd53" , 
     "grant_type":"authorization_code" ]) 
     .responseJSON { response in 
      print(response.request) // original URL request 
      print(response.response) // URL response 
      print(response.data)  // server data 
      print(response.result) // result of response serialization 

      if let JSON = response.result.value { 
       print("JSON: \(JSON)") 
     } 
    } 
} 

但是,這是我所得到的:

Optional(<NSMutableURLRequest: 0x7f9cf959f700> { URL:  https://unsplash.com/oauth/authorize }) 
    Optional(<NSHTTPURLResponse: 0x7f9cf941f920> { URL: https://unsplash.com/oauth/authorize } { status code: 422, headers { 
"Accept-Ranges" = bytes; 
Connection = "keep-alive"; 
"Content-Length" = 8440; 
"Content-Type" = "text/html; charset=utf-8"; 
Date = "Thu, 18 Aug 2016 15:24:30 GMT"; 
Server = Cowboy; 
"Strict-Transport-Security" = "max-age=31536000"; 
Via = "1.1 vegur, 1.1 varnish"; 
"X-Cache" = MISS; 
"X-Cache-Hits" = 0; 
"X-Ratelimit-Limit" = 100; 
"X-Ratelimit-Remaining" = 94; 
"X-Request-Id" = "f00ae2f9-6369-4526-9b28-283c294e2a30"; 
"X-Runtime" = "0.012134"; 
"X-Served-By" = "cache-sin6920-SIN"; 
"X-Timer" = "S1471533870.429494,VS0,VE550"; 
} }) 
    Optional(<3c21444f 43545950 45206874 6d6c3e0a 3c68746d 6c3e0a3c 68656164 3e0a2020 ....2e 636f6d2f 616e616c 79746963 732e6a73 272c2767 6127293b 0a0a2020 20206761 28276372 65617465 272c2027 55412d33 36303439 3637302d 34272c20 27617574 6f27293b 0a202020 20676128 2773656e 64272c20 27706167 65766965 7727293b 0a202020 20676128 2773656e 64272c20 27657665 6e74272c 20276163 74696f6e 272c2027 34323227 293b0a20 203c2f73 63726970 743e0a0a 3c2f626f 64793e0a 3c2f6874 6d6c3e0a>) 
    FAILURE 

如何我應解決這個錯誤?

+0

我仍然試圖找出這一點。沒有成功。 – 209135

回答

2

你知道了嗎?如果沒有,我會給你一個提示。按照我的步驟,您將從Unsplash返回Access-Token獲得成功。

首先,我們將找出如何使用這個請求https://unsplash.com/oauth/authorize。有了這個請求,你需要使用UIWebView,因爲這會返回一個HTML頁面。

用UIWebView試試這個https://unsplash.com/oauth/authorize?client_id=Your_App_ID&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=public+read_user+write_user+read_photos+write_photos+write_likes+read_collections+write_collections

利用UIWebView的你會得到一個代碼,這個請求後返回這樣https://unsplash.com/oauth/authorize/f2ad36deb30b37a2614898e366b84e7c0f68fccd0358565cfbdc2637e*****

代碼後/授權/是你需要與要求https://unsplash.com/oauth/token填寫您的code參數代碼 - 現在你可以使用Alamofire獲得你的令牌。

只是給你提問,如果你還有問題

+0

我放棄了Unsplash API並開始使用pixabay API,因爲我在這個問題上浪費了很多時間。儘管謝謝你的回答。一旦我遇到了其他問題,我將實施您的解決方案。 – 209135