question

julesanchez avatar image
julesanchez asked

unsupported_grant_type when requesting access_token with PHP

Hi, I tried to adapt your JAVA code in php, but the server respond with an error here my code : [i]$body = "grant_type=" + utf8_encode('client_credentials') + "&scope=" + utf8_encode("messaging:push") + "&client_id="+ utf8_encode(MY_CLIENT_ID) +"&client_secret="+ utf8_encode(MY_CLIENT_SECRET); $fp = curl_init(); curl_setopt($fp, CURLOPT_URL, " https://api.amazon.com/auth/O2/token"); curl_setopt($fp, CURLOPT_HEADER, 1); curl_setopt($fp, CURLOPT_POST, 1); curl_setopt($fp, CURLOPT_SSLVERSION,3); curl_setopt($fp, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($fp, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($fp, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'charset=UTF-8') ); curl_setopt($fp, CURLOPT_POSTFIELDS, $body); $response = json_decode(curl_exec($fp),true);[/i] and the response i got from server : [i]HTTP/1.1 400 Bad Request Date: Tue, 02 Jul 2013 14:19:06 GMT x-amzn-RequestId: 5a867343-e322-11e2-b015-29cef268cff2 Content-Type: application/json Content-Length: 130 Vary: Accept-Encoding,User-Agent nnCoection: close {"error":"unsupported_grant_type","error_description":"The authorization grant type is not supported by the authorization server"} [/i] What am I doing wrong ?
amazon device messaging
10 |5000

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

julesanchez avatar image
julesanchez answered
Ok my bad, replace all "+" by "." and code is working. I think i'm doing too much Javascript in this times.. I share my final code, for people interested by it : [i]$body = "grant_type=". utf8_encode('client_credentials'). "&scope=" .utf8_encode("messaging:push"). "&client_id=". utf8_encode(self::CLIENT_ID) ."&client_secret=". utf8_encode(self::CLIENT_SECRET); $fp = curl_init(); curl_setopt($fp, CURLOPT_URL, " https://api.amazon.com/auth/O2/token"); curl_setopt($fp, CURLOPT_HEADER, 1); curl_setopt($fp, CURLOPT_POST, 1); curl_setopt($fp, CURLOPT_SSLVERSION,3); curl_setopt($fp, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($fp, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($fp, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded', 'charset=UTF-8') ); curl_setopt($fp, CURLOPT_POSTFIELDS, $body); $response = json_decode(curl_exec($fp),true);[/i]
10 |5000

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

Sujoy@Amazon avatar image
Sujoy@Amazon answered
Hi Julesanchez, Thank you for posting. Good to know that you have resolved your problem. Basically, it's a common mistake happens in php to use "+" instead of "." as String append operator. Never mind.
10 |5000

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