Google Contacts Data API gives 500 errors

I use OAUTH2 authentication to import GMAIL contacts. In my code, I save the configuration settings in an array:

$clientid=<my Client id>; $clientsecret=<my client secret>; $redirecturi=<my redirect URL>; $max_results = 25; 

Below I create a POST array:

 $fields=array( 'code'=> urlencode($auth_code), 'client_id'=> urlencode($clientid), 'client_secret'=> urlencode($clientsecret), 'redirect_uri'=> urlencode($redirecturi), 'grant_type'=> urlencode('authorization_code') ); $post = ''; foreach($fields as $key=>$value) { $post .= $key.'='.$value.'&'; } $post = rtrim($post,'&'); 

Now I am making a CURL call to get the access token:

 $curl = curl_init(); curl_setopt($curl,CURLOPT_URL,'https://accounts.google.com/o/oauth2/token'); curl_setopt($curl,CURLOPT_POST,5); curl_setopt($curl,CURLOPT_POSTFIELDS,$post); curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0); $result = curl_exec($curl); curl_close($curl); $response = json_decode($result); $accesstoken = $response->access_token; log_message('debug','POST'.$post); log_message('debug','contents'.$result); 

This is where I have problems. For the first callback, this request token access process returns an Error 500 response, which appears in my logs as follows:

 <HTML> <HEAD> <TITLE>Error processing OAuth 2 request</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Error processing OAuth 2 request</H1> <H2>Error 500</H2> </BODY> </HTML> 

But for all subsequent callbacks, I get the appropriate access token.

What is going on for the first time?

Update I somehow reset my client secret, and for a while I got the correct XML response. But, suddenly, from the blues, he began to give me a new mistake.

Immediately after calling curl, to get access_token , it shows me {"error": "invalid_grant"}. Why is this so?

+1
source share
1 answer

You will get invalid_grant if your $auth_code has already been used. Authorization codes can only be used once.

0
source

Source: https://habr.com/ru/post/1389002/


All Articles