question

newuser-602b0c78-1658-45b6-86c0-266c86cf86b9 avatar image
newuser-602b0c78-1658-45b6-86c0-266c86cf86b9 asked ·

Login with Amazon button is not working in my website [error: unsupported_grant_type]

I'm trying to implement 'Login with amazon' button in my website. following are my code.

window.onAmazonLoginReady = function() {
      amazon.Login.setClientId('amzn1.application-oa2-client.XXXXXXXXXXXXXXXXXXXXXX');
};
(function(d) {
  var a = d.createElement('script'); a.type = 'text/javascript';
  a.async = true; a.id = 'amazon-login-sdk';
  a.src = 'https://assets.loginwithamazon.com/sdk/na/login1.js';
  d.getElementById('amazon-root').appendChild(a);
})(document);
document.getElementById('LoginWithAmazon').onclick = function() {
  options = {};
  options.scope = 'profile';
  options.response_type = 'code';
  amazon.Login.authorize(options, 'https://web.opticatest-online.de/AmazonSignUpVerification.php');
  return false;
};

After executing above code, it redirects to "https://web.opticatest-online.de/AmazonSignUpVerification.php?code=ANsDJOFowJZYAjfyolQl&scope=profile"
and having "code" and "Scope" as parameter.

By referring this Doc implemented the code in php to fetch "access_token" through curl call like below


if(isset($_REQUEST['code'])  && !empty($_REQUEST['code'])){
    $c = curl_init('https://api.amazon.com/auth/o2/token');
    // $c = curl_init('https://api.amazon.com/auth/o2/token?grant_type=authorization_code&code='.$_REQUEST['code'].'&redirect_uri=https://web.opticatest-online.de/AmazonSignUpVerification.php&client_id=amzn1.application-oa2-client.xxxxxx&client_secret=8d2dbdc66ffffffffffffxxxxxxxxxxxxxxxxx');
    curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($c, CURLOPT_HTTPHEADER, 
        array(    'Authorization: Basic czzCaGRSa3F0MzpnWDFmQmF0M2JW',
                'Content-Type: application/x-www-form-urlencoded;charset=UTF-8',
                'client_id: amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
                'client_secret: 8d2dbdcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxf04d72a8')
    );
    $data = array(
        'grant_type'=>'authorization_code', 
        'code' => $_REQUEST['code'],
        'redirect_uri' => 'https://web.opticatest-online.de/AmazonSignUpVerification.php',
        'client_id'=>'amzn1.application-oa2-client.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'client_secret'=>'8d2dbdc66a4xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx2a8'
    );
    $payload = json_encode($data);
    curl_setopt($c, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
    $r = curl_exec($c);
    if(curl_errno($c)){
        echo 'Request Error:' . curl_error($c);
    }
    curl_close($c);
    $d = json_decode($r);
    print_r($d);
    echo "string";
}else{
    print_r($_REQUEST);
}


but i'm getting error like "unsupported_grant_type"

stdClass Object (    [error_description] => The authorization grant type is not supported by the authorization server    [error] => unsupported_grant_type )

NOTE: I tried both GET and POST methods. In GET method getting empty page and for POST method the above error raising.

Any Luck?

login with amazonbuttons
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

0 Answers