Jon Harris Posted November 12, 2021 Posted November 12, 2021 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_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $content )); $response = curl_exec($curl); curl_close($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.
Jon Harris Posted November 12, 2021 Author Posted November 12, 2021 (edited) 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 November 12, 2021 by Jon Harris 1
Administrators Ben Smith Posted November 12, 2021 Administrators Posted November 12, 2021 Glad you're up and running Jon - also appreciate you coming back to update with the solution! Ben
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now