2016-04-11 1 views
0

私の質問は、Previous questionで以前に質問された質問の行に沿っています。io9 - Alamofireトークン要求が401エラーで失敗するdebugpsrintのdepsite curlが動作する

私はAlamofire 3.2.1、XCode 7.3、iOS9を使用しています。ここで

は、DjangoのOAuthのツールキットで実装OAuthのサーバ(ローカルホスト)からトークンを取得する意思を持つ私のコードです:

let params = ["grant_type": "password", 
        "username": "rosa", 
        "password": "rosa1234"] 

    let request = Alamofire.request(.POST, url, parameters: params).authenticate(user: __clientID, password: __clientSecret) 

    debugPrint(request) 

    request.validate(statusCode: 200..<300) 
     .validate(contentType: ["application/json"]) 
     .responseSwiftyJSON({ (request, response, json, error) in 
      debugPrint(response) 

      if (error != nil) { 
       print(error.debugDescription) 
       return completionHandler(token: nil, error: error) 
      } 

      // call the completionHandler function (object) to deal with data further 
      return completionHandler(token: json.string, error: nil) 
     }) 

リクエストのデバッグ出力はコマンドで動作取り組んでcurlコマンドを与えます行:

curl -i \ 
-X POST \ 
-u PrtRUN9ra7LHCYWbiReaAjO9I26lJhLhRSAUJgtr:kTvxqKmClDAL3tbdyZdyBZgsfsfXtagMpZyFjSZwpIknxM43l6ZIvJxJGXu2J2FuHf4JMLfopDoAzkF6vHSRq4GZkbnEZSmmUnMvkhMvSucbhWUdzCpxuj9qtc8beaQ3 \ 
-H "Content-Type: application/x-www-form-urlencoded; charset=utf-8" \ 
-H "User-Agent: IntelliCafe/xsoft.IntelliCafe (1; OS Version 9.3 (Build 13E230))" \ 
-H "Accept-Encoding: gzip;q=1.0, compress;q=0.5" \ 
-H "Accept-Language: en-US;q=1.0" \ 
-d "grant_type=password&password=rosa1234&username=rosa" \ 
"http://localhost:8000/o/token/" 

しかし、コードは私のアプリでは機能しません。エラーは401です:

Optional(<NSHTTPURLResponse: 0x7d183950> { URL: http://localhost:8000/o/token/ } { status code: 401, headers { 
    "Cache-Control" = "no-store"; 
    "Content-Type" = "application/json"; 
    Date = "Mon, 11 Apr 2016 20:43:48 GMT"; 
    Pragma = "no-cache"; 
    Server = "WSGIServer/0.1 Python/2.7.10"; 
    "X-Frame-Options" = SAMEORIGIN;} }) 
Optional(Error Domain=com.alamofire.error Code=-6003 "Response status code was unacceptable: 401" UserInfo={NSLocalizedFailureReason=Response status code was unacceptable: 401}) 

この問題は私を狂っています。どんな助けでも感謝します!

ありがとうございます。

答えて

0

私はdjango rest viewを書いて、リクエストのメタデータを表示し、clientIDとclientSecretがdjangoサーバ実装によって適切に埋め込まれていないことを知りました。

Alamofireのドキュメントに示されているようにAuthorizationヘッダーを使用すると動作します。

関連する問題