Google OAuth2 error - missing required parameter: grant_type on update

I have prototyped a calendar sync system using the google calendar API and it works well except for refreshing access tokens. These are the steps I went through:

1) Authorized my API and got an authorization code.

2) Exchange authorization code for Access Token and RefreshToken.

3) Used by the calendar API until the access token expires.

At this point, I'm trying to use the refresh token to get another access token, so my users don't need to be granted access because the diary sync happens when they are offline.

Here's the PHP code, I am using curl requests all over the system.

$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = array("grant_type" => "refresh_token", 
                  "client_id" => $clientID,    
                  "client_secret" => $clientSecret, 
                  "refresh_token" => $refreshToken);

$headers[0] = 'Content-Type: application/json';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));

$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);

      

The answer I get is:

[error] => invalid_request
[error_description] => Required parameter is missing: grant_type

      

No curling errors reported.

I've tried content-type header: application / x-www-form-urlencoded and more with the same result.

I suspect this is something obvious in my settings or curl headers, since every parameter mentioned in the google documentation for this request is set. However, I am going in circles, so any help would be appreciated, including pointing out obvious mistakes that I forgot.

+3


source to share


1 answer


your request should not be posting JSON data, but rather data in the form of a request, as in:



$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = "grant_type=refresh_token&client_id=$clientID&client_secret=$clientSecret&refresh_token=$refreshToken";

$headers[0] = 'Content-Type: application/x-www-form-urlencoded';

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);

      

+5


source







All Articles