skip to Main Content

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%3A%2F%2Fexample.test%2F&scope=user-read-currently-playing%20user-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%3A%2F%2Fexample.test%2F 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?

2

Answers


  1. Chosen as BEST ANSWER

    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
      ]
    );
    

  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search