laravel - Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' -
i working package analytics-laravel 4 google analytics , have follower of steps correctly. when try site id example, face error:
error refreshing oauth2 token, message: '{ "error" : "invalid_grant" }'
i have double checked of configurations, client id, service_account , private key error still occurs. else should try check maybe solve issue?!
i didn't use package before, i'm using google-api-php-client, anyways, error occurs if don't set refresh token.
you should know need have access token once. need set access type offline, provide refresh token use automatically new access token without getting new code every time access token expires.
in google's console, created client id web application. make sure set redirect uri web page receive code , extract access token using code.
here code example using google-api-php-client, hope help:
you need run following code once, , retrieve , store access token.
<?php require_once('google-api-php-client-master/src/google/client.php'); session_start(); $client = new google_client(); $client->setapplicationname('app_name'); $client->setclientid(your_client_id); $client->setclientsecret('your_client_secret'); $client->setredirecturi('your_redirect_uri'); $client->setdeveloperkey('your_dev_key'); $client->setscopes(array('https://www.googleapis.com/auth/analytics.readonly')); $client->setaccesstype("offline"); // step 1: create auth url if (isset($_get['ref']) && $_get['ref'] == "1") { $authurl = $client->createauthurl(); return redirect::to($authurl); } // step 2: user accepted access need exchange it. if (isset($_get['code'])) { $client->authenticate($_session['code']); //authenticate client $token = $client->getaccesstoken(); //get access token var_dump($token); //store token in db or config file die(); } ?>
after getting access token code above (which should contain refresh token), store in db or config file.
now following code should authenticate client , refresh access token when expires via getaccesstoken function
<?php require_once('google-api-php-client-master/src/google/client.php'); require_once('google-api-php-client-master/src/google/service/analytics.php'); $client = new google_client(); $client->setapplicationname('app_name'); $client->setclientid(your_client_id); $client->setclientsecret('your_client_secret'); $client->setredirecturi('your_redirect_uri'); $client->setdeveloperkey('your_dev_key'); $client->setscopes(array('https://www.googleapis.com/auth/analytics.readonly')); $client->setaccesstype("offline"); //make sure access type offline refresh token $config = coreconfig::find(1); //getting first record config table $client->setaccesstoken($config->google_access_token); //retrieve access token stored , set client object //check token expired if($client->isaccesstokenexpired()) { $token = json_decode($config->google_access_token, true); //get token stored, , convert json array $client->refreshtoken($token['refresh_token']); //set refresh token $newtoken = $client->getaccesstoken(); //call getaccesstoken() function new access token $config->update(array('google_access_token' => $newtoken)); //store new token in db } if ($client->getaccesstoken()) { $analytics = new google_service_analytics($client); //do $analytics object } ?>
Comments
Post a Comment