Jump to content

Refresh Token Problem with PHP


Recommended Posts

I have sucessfully used Postman on  my desktop to generate tokens and make API calls. However, I am really stuck trying to use the returned "refresh_token" to get a new access token. I can't get it working directly in postman or php.

If I copy-paste the "access_token" Postman retreived into my php code to "get_addresses" it works fine (but obviously stops working after 5 mins). However, if I copy-paste the "refresh_token" into my php code to refresh the access token (its a different file). I get:

"error":"invalid_client","error_description":"Parameter 'client_id' and/or 'client_secret' are invalid."

Here is the code block:

     // define params
     $client_id = "f9xxxx";
     $client_secret = "?xxxxxxxx";
     $refresh_token= "<extracted from the original postman call>" ;
     $token_url = "https://oauth.accounting.sage.com/token";
     $header = array('Accept: application/json','Content-Type: application/x-www-form-urlencoded');
     $content = "client_id=$client_id&client_secret=$client_secret&grant_type=refresh_token&refresh_token=$refresh_token";

     $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_URL => $token_url,
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $content
    $response = curl_exec($curl);
    echo $response;

Incidentally, I did try it without the accept array element and it made no difference. I also tried this directly in Postman by creating a new POST called "token renewal", but that didn't work either. I am missing something?

Any pointers appreciated.


Link to comment
Share on other sites

The method was fine. I just need to url encode the params. Working now.

$refresh_token_enc =  urlencode($refresh_token) ;
$client_id_enc =  urlencode($client_id) ;
$client_secret_enc = urlencode($client_secret) ;

$token_url = "https://oauth.accounting.sage.com/token";
$header = array('Accept: application/json','Content-Type: application/x-www-form-urlencoded');
$content = "client_id=$client_id_enc&client_secret=$client_secret_enc&grant_type=refresh_token&refresh_token=$refresh_token_enc"
Edited by Jon Harris
  • Like 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in

Sign In Now

  • Create New...