Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "The authorization grant type is not supported by the authorization server" from amazon

I am trying getting access token no i'm getting this error

{"error_description":"The authorization grant type is not supported by the authorization server","error":"unsupported_grant_type"}

$code =  $_GET['code'];

$postfields = array(
    'grant_type'=>'authorization_code',
    'code'=>$code,
    'redirect_uri='=>'example/myTest.php',
    'client_id'=>'amzn1.application-oa2-client.xxxxxxxxxxx',
    'client_secret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
    );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.amazon.com/auth/o2/token');
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/x-www-form-urlencoded']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($httpRequest, CURLOPT_HEADER, 1);
// Edit: prior variable $postFields should be $postfields;
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // On dev server only!
$result = curl_exec($ch);

print_r($result);
like image 702
Gev Webf Avatar asked Nov 15 '16 09:11

Gev Webf


2 Answers

Change CURLOPT_HTTPHEADER from

Content-Type: application/x-www-form-urlencoded

to

Content-Type: application/json

like image 78
Mohamed El Tamimy Avatar answered Sep 29 '22 05:09

Mohamed El Tamimy


Try to add 'token_type' => 'bearer' to your $postfields data.

Here is from Amazon developer documentation: "Access Token Request ... The type of token returned. Should be bearer."

"Access Token Response: ... unsupported_grant_type The client specified the wrong token_type."

like image 43
SergeyLebedev Avatar answered Sep 29 '22 07:09

SergeyLebedev