Spotify api request access token issue

I’m trying to request the access token from the spotify API, I made it thru the first part with logging in, but I cant figure out the correct curl.

This is my code

    $auth_token = $_GET['code'];
    $clientSecret = '11111111111';
    $url = 'https://accounts.spotify.com/api/token';
    $clientID = '22222222222';
    $redirect_uri = 'https://www.mydomain.com/test/spotify';
        
    $oauthFields = array(
        'client_id' => $clientID,
        'client_secret' => $clientSecret,
        'grant_type' => 'authorization_code',
        'code' => $auth_token,
        'redirect_uri' => $redirect_uri);
    

    $parameters = http_build_query($oauthFields);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url.$parameters);
    curl_setopt($ch, CURLOPT_POST, count($oauthFields));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)');

    $urlResponse = curl_exec($ch);
    curl_close($ch); 
    $spotify = json_decode($urlResponse);

https://developer.spotify.com/documentation/general/guides/authorization-guide/

Ok and what is the value of $urlResponse after you call this? What is spotify saying to you in the response? You might also find out other info from using curl_getinfo($ch). Things like the HTTP status might help here to tell you if you are just getting an unauthorized or if you are getting a 200 OK but that the response coming back is not what you expect.

Extra note: Also make sure that the redirect_uri is the same value as you use when requesting your authorization code. This sometimes trips people up.

Let us know. :slight_smile:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.