【问题标题】:Spotify API - error retrieving access token during authorization processSpotify API - 在授权过程中检索访问令牌时出错
【发布时间】:2016-07-24 02:50:38
【问题描述】:

我正在构建我的第一个 Spotify 应用程序,现在我正在处理授权过程。

到目前为止,我已经成功地从https://accounts.spotify.com/authorize 检索到我的状态和代码

现在我通过 PHP CURL 请求发送 POST 请求以获取我的访问令牌。

Spotify's instructions for this step

我不断收到以下 JSON 错误响应,表明我的 grant_type 无效,它为我提供了三个有效选项:

{"error":"unsupported_grant_type","error_description":"grant_type 必须是 client_credentials、authorization_code 或 refresh_token"}bool(true)

如果您查看下面的代码,我相信我已经设置了正确的“authorization_code”授权类型,但我收到了错误。我用 '******' 突出显示了我认为正确的代码行的代码 sn-p。

谁能看到我做错了什么?这是我用来发送请求的代码:

    //  Get access tokens
        $ch = curl_init();              
        // Specify the HTTP headers to send.
                //Authorization: Basic <base64 encoded client_id:client_secret>
                $ClientIDSpotify = "[my spotify app id]";
                $ClientSecretSpotify = "[my secret code]";
                $authorization = base64_encode ( "{$ClientIDSpotify}:{$ClientSecretSpotify}" );
                $http_headers = array( 
                            "Authorization: Basic {$authorization}"
                        );
                curl_setopt( $ch, CURLOPT_HTTPHEADER, $http_headers );
        curl_setopt( $ch, CURLOPT_POST, true);

        $spotify_url = "https://accounts.spotify.com/api/token";
        curl_setopt( $ch, CURLOPT_URL, $spotify_url );



        // *************************************************
        // HERE'S WHERE I CORRECTLY SPECIFY THE GRANT TYPE
        // *************************************************
        $data['grant_type'] = "authorization_code";
        $data['code'] = $authorizationCode;
        $callbackURL = "[my callback URL]";     
        $data['redirect_uri'] = $callbackURL;    

        curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        $response_json = curl_exec( $ch );
        curl_close( $ch );
    }

【问题讨论】:

    标签: php curl authorization token spotify


    【解决方案1】:

    就像上一条关于切换到 http_build_query 的评论的注释一样,我必须 URLDECODE 数据,以便 Spotify 识别它。尝试改用这条线。

    curl_setopt($ch, CURLOPT_POSTFIELDS, urldecode(http_build_query($data)));
    

    【讨论】:

      【解决方案2】:

      在我看来,POST 正文的格式不正确,其他一切看起来都不错。

      我对 PHP 的有限理解告诉我,你的 POST 正文看起来像

      { "fields" : { "code" : $authorizationCode, "grant_type" : "authorization_code", "redirect_uri" : "http://www.example.com/spotify/callback/index.php" } }

      当然,你要发送的只是

      { "code" : $authorizationCode, "grant_type" : "authorization_code", "redirect_uri" : "http://www.example.com/spotify/callback/index.php" }

      因此,尝试将$data 对象设置为

      $data['grant_type'] = "authorization_code"; $data['code'] = $authorizationCode; $data['redirect_uri'] = $callbackURL;

      甚至更短

      $data = array("grant_type" => "authorization_code", "code" => $authorizationCode, "redirect_uri" => $callbackURL);

      希望这有帮助!

      【讨论】:

      • 谢谢,迈克尔,我会看看这是否会改变事情。
      • 能否将 json_encode($data) 的输出添加到您的问题中?
      • {"grant_type":"authorization_code","code":"[long code]","re​​direct_uri":"[callback URL]"}"
      【解决方案3】:

      好的,所以我挖了一点,在 PHP CURL 手册 cmets 部分找到了一些代码。 Spotify 文档的问题在于它没有指定要发送的 POST 数据的格式。我假设由于 Spotify 向我发送 JSON 数据,我也应该以 JSON 格式发送我的数据。所以我正在格式化 POST 数据:

              curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
      

      在阅读了一些文档后,我决定试试这个:

              curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
      

      我得到了我需要的结果:

              {"access_token":"[long access token]","token_type":"Bearer","expires_in":3600,"refresh_token":"[long refresh token]"}
      

      感谢您,Michael,您尝试提供帮助!

      【讨论】:

        猜你喜欢
        • 2015-11-11
        • 2020-07-13
        • 1970-01-01
        • 2019-05-09
        • 2020-01-09
        • 2019-07-22
        • 1970-01-01
        • 1970-01-01
        • 2018-10-31
        相关资源
        最近更新 更多