Announcements

Help Wizard

Step 1

NEXT STEP

FAQs

Please see below the most popular frequently asked questions.

Loading article...

Loading faqs...

VIEW ALL

Ongoing Issues

Please see below the current ongoing issues which are under investigation.

Loading issue...

Loading ongoing issues...

VIEW ALL

Documentation mistake or bug

Documentation mistake or bug

This authorization request works for me in php-curl:

 

 

 

 $ch = curl_init();

    //Escribo endpoint el indicado por api
    curl_setopt($ch, CURLOPT_URL,            'https://accounts.spotify.com/api/token');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    //IMPORTANTE ES UNA PETCION POST NO GET, TAL COMO INDICA DOCUMENTACION API
    curl_setopt($ch, CURLOPT_POST,           1);

    //Esta forma de añadir los parametros NO LE GUSTA a spotify, aunque deberia funcionar....seguramente algo hace 
    //http_build_query  It returns a URL-encoded string.
    //  curl_setopt($ch, CURLOPT_POSTFIELDS,     'code=' . $_REQUEST['code'] );
    //  curl_setopt($ch, CURLOPT_POSTFIELDS,     'redirect_uri=' . $redirect_uri );
    //  curl_setopt($ch, CURLOPT_POSTFIELDS,     'grant_type=authorization_code');

    //Esta forma de añadir los parametros SI LE GUSTA a spotify..
    //ojo estos parametros como indica la API van en el body de la peticion.
    $params = array('code' => $_REQUEST['code'], 'redirect_uri' => $redirect_uri, 'grant_type' => 'authorization_code');
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));

    //ojo estos parametros como indica la api van el el header de la peticion.
    //ojo documentación dice que no va aqui el grant-type pero si no no funciona...
    curl_setopt($ch, CURLOPT_HTTPHEADER,     array('Authorization: Basic ' . base64_encode($client_id . ':' . $client_secret), 'Content-type: application/x-www-form-urlencoded', 'grant_type=authorization_code'));

    //resultado de peticion
    $result = curl_exec($ch);

    //Gestion de errores
    if (curl_errno($ch)) {
        print "Error: " . curl_error($ch);
        echo 'Error en ch curl';
    } else {
        // Show me the result Info de la peticion CURL ...se podria quitar mas adelante
        echo 'ch curl Info';
        echo '<h4> Info Curl </h4>';
        $info = curl_getinfo($ch);

        echo '</br>';
        echo '<pre>' . print_r($info, true) . '</pre>';
        // echo 'Took ', $info['total_time'], ' seconds to send a request to ', $info['url'], "\n";
        //El resultado de la consulta que devuelve un json, se pasa a variable php
        //json_decode Convierte un string codificado en JSON a una variable de PHP. 
        $json = json_decode($result);

        //Resultado consulta...puedo acceder de la forma json->access_token
        echo '</br>';
        echo '<h2> Resultado consulta </h2>';
        echo '<pre>' . print_r($json, true) . '</pre>';
        
    }

    //Cierro CURl
    curl_close($ch);

 

 

 

The main problem came in:

 

 

 

 

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode($client_id . ':' . $client_secret), 'Content-type: application/x-www-form-urlencoded', 'grant_type=authorization_code'));

 

 

 

 

as documentation says it has to have authorization and content-type. Nothing says about grant-type in headers.

So or its a bug or documentation is missing, grant type in header of request.

 

Hope it can be solved soon, i spent twor or three days....

Reply
2 Replies

Any help or solution please ? Thanks

hello did you find any solution

Suggested posts