I am writing following code to get authorization token.
assigning authorization to code variable
$code = $this->request->query['code'];
i have deleted my client secret to post question.
$clientSecret = "";
initializing curl
$ch = curl_init();
$header[] = "Content-Type: application/x-www-form-urlencoded";
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode("code=".$code."&
client_id=289710071999.apps.googleusercontent.com&
client_secret=".$clientSecret."&
redirect_uri=http://budzzflorist.localhost.com/users/google&
grant_type=authorization_code"));
curl_setopt($ch, CURLOPT_URL, "https://accounts.google.com/o/oauth2/token");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$request_result = curl_exec($ch);
print_r(curl_getinfo($ch));
echo "\n\ncURL error number:" .curl_errno($ch); // print error info
echo "\n\ncURL error:" . curl_error($ch);
var_dump( json_decode($request_result,true));
It looks like Google wants the form to be
application/x-www-urlencoded.Try this:
Result:
invalid_clientbecause my tokens are incorrect, but notinvalid_request.If you pass an array to
CURLOPT_POSTFIELDS, cURL will force the content type to bemultipart/form-datawhich is what causes invalid request. Instead usehttp_build_querywhich creates a url-encoded query string from an array based on key-value pairs. Then pass the result of that toCURLOPT_POSTFIELDS.