简体   繁体   中英

google+ api not working

My google+ api code was working. Suddenly I get an error on the following line. Any help is appreciated.

if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}

The error message is as follows

 Warning: json_decode() expects parameter 1 to be string, array given in /opt/bitnami/apache2/htdocs/goo/src/auth/apiOAuth2.php on line 142 Fatal error: Uncaught exception 'apiAuthException' with message 'Could not json decode the access token' in /opt/bitnami/apache2/htdocs/goo/src/auth/apiOAuth2.php:144 Stack trace: #0 /opt/bitnami/apache2/htdocs/goo/src/apiClient.php(186): apiOAuth2->setAccessToken(Array) #1 /opt/bitnami/apache2/htdocs/goo/googlep.php(1196): apiClient->setAccessToken(Array) #2 {main} thrown in /opt/bitnami/apache2/htdocs/goor/src/auth/apiOAuth2.php on line 144

I had the same error too a while ago. Actually this function setAccessToken only accepts json_encoded string ( simply saying the json encoded string that you get when user authenticates which has access token, refresh token, bearer etc etc). You have to pass that json encoded string to this function to set the access token . $client = new Google_Client();

$token = $client->getAccessToken(); // get the token when google redirects the user after authentication to your redirect uri with the code. // $token will have json encoded string.

$client->setAccessToken($token); // now set the access token here with that $token

if you pass an array or anything else json encoded string to setAccessToken you will get this error.

Your error can be because of $_SESSION['access_token'] . I was working with gplus and twitter api once, and both of them were using the that variable $_SESSION['access_token'] .

Just modify in your google plus code the access_token to access_token_plus or whatever you like so you be able to avoid the rewrite conflict.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM