Home > Blockchain >  Can’t get a refreshed access_token using spotify api and laravel
Can’t get a refreshed access_token using spotify api and laravel

Time:12-31

First time using HTTP Client and Spotify API in Laravel.

The first step is to get a code by visiting https://accounts.spotify.com/authorize?client_id=c1990...deed3&response_type=code&redirect_uri=http://example.test/&scope=user-read-currently-playing user-top-read

I then copy the code from the url after being redirected.

Then using curl -

curl -H "Authorization: Basic YzE5OT...Q2ZjA=" -d grant_type=authorization_code -d code=AQBX...X5zg -d redirect_uri=http://example.test/ https://accounts.spotify.com/api/token

This returns the refresh token in JSON format -

{ "access_token":"BQBQL...vNDQ", "token_type":"Bearer", "expires_in":3600, "refresh_token":"AQCy...areM", "scope":"user-read-currently-playing user-top-read" }

But then I can't seem to get an access token using the refresh_token.

I'm getting "Bad Request" statusCode: 400 in my app

$response = Http::withHeaders([
    'Authorization' => `Basic YzE5O...Q2ZjA`,
    'Content-Type' => 'application/x-www-form-urlencoded'
])
    ->asForm()
    ->post(
        'https://accounts.spotify.com/api/token',
        [
            'grant_type' => 'refresh_token',
            'refresh_token' => 'AQC7...YphY'
        ]
    );

Here is the documentation https://developer.spotify.com/documentation/general/guides/authorization/code-flow/.

Has anyone implemented this before in Laravel and if so how?

CodePudding user response:

I have no idea about the Spotify API, but I am 99% sure your error is ->asForm(), you are sending that as a form instead of a normal request... so your code may need to be like this:

$response = Http::withToken('YzE5O...Q2ZjA')
    ->post(
        'https://accounts.spotify.com/api/token',
        [
            'grant_type' => 'refresh_token',
            'refresh_token' => 'AQC7...YphY'
        ]
    );

See that I have removed ->asForm() and Content-Type (not sure why you are using that, it is a normal API... and ->asForm() already sets the same content you have manually set...

This is the Spotify API and I do not see any need to set the Conetnt-Type.


My bad, you need to set the ->asForm(), so the code should be:

$response = Http::withToken('YzE5O...Q2ZjA')
    ->asForm()
    ->post(
        'https://accounts.spotify.com/api/token',
        [
            'grant_type' => 'refresh_token',
            'refresh_token' => 'AQC7...YphY'
        ]
    );

But I still think you are missing something. Check that your refresh_token is correct. Also lookf for more debugging output

CodePudding user response:

This worked for me.

'Content-Type' => 'application/x-www-form-urlencoded' was removed from withHeaders

$response = Http::withHeaders([
  'Authorization' => 'Basic ' . 123...123,
])
->asForm()
->post(
  'https://accounts.spotify.com/api/token',
  [
    'grant_type' => 'refresh_token',
    'refresh_token' => 123...456
  ]
);
  • Related