2017-11-15 9 views
0

はその認証コードのフローのために彼らのAPIを打つための私の方法です:トークンのAPIをSpotifyに送信するとステータス415が返されます。何か不足していますか?ここ

  • 変更する:下の方に

    class func obtainAuthTokenPackage(authCode: String) throws 
    { 
        //Create a request 
        var request = URLRequest(url: Gimme.theSpotify.urlFor(endpoint: .requestingTokens)) //"https://accounts.spotify.com/api/token" 
        request.httpMethod = "POST" 
    
        //Build the header 
        let spotifyClientCreds = Gimme.theSpotify.clientID + ":" + Gimme.theSpotify.clientSecret 
        let encodedCreds = spotifyClientCreds.data(using: .utf8)!.base64EncodedString() 
        request.setValue("Basic \(encodedCreds)", forHTTPHeaderField: "Authorization") 
        request.addValue("application/json", forHTTPHeaderField: "Content-Type") 
    
        //Build the body 
        var dict = [String:String]() 
        dict["grant_type"] = "authorization_code" 
        dict["code"] = authCode 
        dict["redirect_uri"] = Gimme.theSpotify.redirectURI 
        var package = Data() 
        do 
        { 
         package = try JSONSerialization.data(withJSONObject: dict) 
        } 
        catch 
        {print("oopsie")} 
        request.httpBody = package 
    
        //Set up a web transaction 
        let transaction = URLSession.shared.dataTask(with: request) { 
         (possData, possResp, possErr) in 
         if let data = possData 
         { 
          print(String(data: data, encoding: .utf8)!) 
         } 
        } 
        //Do it 
        transaction.resume() 
    } 
    

    print文は、私はすでに試した{"error":"server_error","error_description":"Unexpected status: 415"}

    物事を生成しますrequest.setValue(...request.addValue(...とその逆の違いはありません。

  • application/x-www-form-urlencodedを使用して、HTTP本体を"grant_type=authorization_code&code=" + authCode + ...".data(using: .utf8)に変更します。
    これを実行すると、APIは認可タイプをauthorization_codeに設定する必要があるというメッセージで応答します(これは、サーバーが自分のhttpボディを正しく解析していないことを示しています)。

  • (JSONを使用して)ヘッダーから本文にクライアント資格情報を移動します。

  • が成功とその暗黙のグラント・フローを実装休んアプリ(それはポストマンやHTTPRequestorようなものだ)

  • を使用して要求スウィフト4の新しいJSONエンコーディングツールを使用して、私のHTTPボディを作成

  • 。しかし:(それは、リフレッシュトークンを与えるものではありませんし、私はそれを必要とする。ヘッダフィールドを削除content-type

  • Content-Typeを変更

  • は内部すすり泣きので、私は私の周りの人の気をそらすしないでくださいすなわち

  • 率(%2Fでスラッシュ、例えば%3aとコロンを置換)redirect-uriの文字のエスケープコンテンツタイプを指定し私が持っている件の

質問:

  • ステータス415がサポートされていないメディアタイプを意味し、その代わりにJSONのapplication/x-www-form-urlencodedを期待SpotifyはAPIはありますか?

  • SwiftプロジェクトでSpotifyの認証コードフローが機能するようになったら、どうしましたか?

  • application/x-www-form-urlencodedを使用している場合、httpボディをどのように作成しましたか?

ありがとうございました。

+1

はあなたがコードしない試してみましたが安心ですヘッダーの資格情報? –

+0

ああ、そうです。私はそれに言及するのを忘れていた。 –

+0

JSONリクエストボディが期待されています。ガイドの[code flow](https://developer.spotify.com/web-api/authorization-guide/#authorization_code_flow)部分に読み込みを行い、全体的なチェックを「カール」で行いますが、あなたのコードは正しく見えます。 –

答えて

0

私はそれをしました! HAHAHAHAHAHAHAHAHAHA !!!!!!!!!!!私は今、答えることができだということだった

//The endpoint expects form-urlencoded 
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type") 

//Build a string that has client_id, client_secret, grant_type, the code, and the redirect uri 
//It has to be in that order (I didn't try any other order, but I've been having this problem for so long, I don't want to risk anything) 
//It has to be in the form "key=value&key=value..." (Standard form url encoded format) 
var formEncoded = "client_id=\(Gimme.theSpotify.clientID)" 
formEncoded.append("&client_secret=\(Gimme.theSpotify.clientSecret)") 
formEncoded.append("&grant_type=authorization_code") 
formEncoded.append("&code=\(authCode)") 
formEncoded.append("&redirect_uri=\(Gimme.theSpotify.redirectURI)") 

//Finally convert it into UTF8 data 
let bodyData = formEncoded.data(using: .utf8) //Check that this isn't nil, then proceed 

//Stick this stuff in the transaction, and it'll be SUCCESSFULLLLL 

質問:
- このAPIエンドポイントのContent-Typeとしてhttps://accounts.spotify.com/api/tokenapplication/x-www-form-urlencoded
- 私は異なった:クライアントIDとクライアントシークレットを、grant_typeキーの前に入れてください。 - 上記のコードセグメントで示すように手動でhttpボディを作成しました。結論として


-
必要本当に空想何も - SpotifyはAPIドキュメントが不足している(しかし、勘弁してくれよ、だではないか?)
- 私は

関連する問題