繁体   English   中英

Spotify Web API 无法获取访问令牌

[英]Spotify Web API can not get access token

$response = Http::withHeaders([
            'Authorization' => 'Basic '. base64_encode(env('CLIENT_ID').':'.env('CLIENT_SECRET')),
            'Content-Type' => 'application/x-www-form-urlencoded'
        ])->post('https://accounts.spotify.com/api/token', [
            'code' => trim($code),
            'redirect_uri' => env('REDIRECT_URI'),
            'grant_type' => 'authorization_code',
        ]);
print_r($response->json());

这个返回码:

[error] => unsupported_grant_type [error_description] => 当我想获取访问令牌时,grant_type 参数丢失。

我通过使用 Guzzle 客户端库而不是 laravel 的 Http 外观解决了问题。我将选项发送为 form_params。感谢您的回复。

 $client = new GuzzleHttp\Client(); $response = $client->request('POST', 'https://accounts.spotify.com/api/token', [ 'headers' => $headers, 'form_params' => $options ]);

In Laravel, if you expect to use Http client and set the Content-type to "application/x-www-form-urlencoded" just call the asForm() method, like described in Laravel Http documentation (source: https://laravel .com/docs/master/http-client#authentication )。

$response = Http::withHeaders(['Authorization' => 'Basic ' . base64_encode(env('CLIENT_ID') . ':' . env('CLIENT_SECRET'))])
            ->asForm()
            ->post('https://accounts.spotify.com/api/token', [
                'code' => trim($code),
                'redirect_uri' => env('REDIRECT_URI'),
                'grant_type' => 'authorization_code'
            ]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM