AdWords is now Google Ads. Our new name reflects the full range of advertising options we offer across Search, Display, YouTube, and more. Learn more

Analytics
4.6K members online now
4.6K members online now
Dive into multiple domain (Cross/Sub) tracking, implementing Ecommerce and Enhanced Ecommerce, setting up Event tracking, and Universal Analytics code.
 
Guide Me
star_border
Reply

Web Application using the refresh token/access token

Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hi Google Support team,

 

 

Am attempting to create a Web Application using Google Client API. However I cant understand how to use the Google Authorization method where you need to use the refresh token to get a new access token Currently I get a error: Access token expired.

Code------

$client = new Google_Client();
$client->setClientId('########');
$client->setClientSecret('#####');
$client->setRedirectUri('http://localhost:8080/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/analytics.readonly');
$client->setAccessType('offline');

 

$tokenObj = array('access_token' => '#####');
$tokenObj = json_encode($tokenObj);
$client->setAccessToken($tokenObj);

$refreshToken='#########';

 

Re: Web Application using the refresh token/access token

A T
Participant ✭ ✭ ✭
# 2
A T
Participant ✭ ✭ ✭

Re: Web Application using the refresh token/access token

Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭
Hi A T,

Thank you for the response- I tried the provided tutorial link and still
recieve a Access token expired error. I have a the access token an check if
its expired. But not sure how revoke method uses the refresh token to
create new access token.
Can you please guide me on what I have missed/done incorrectly.

Have posted my code below.

Thanks,
Maurice

//Setup Login Information and Authenticate Google object
$client = new Google_Client();
$client->setAuthConfigFile(#####client_secrets.json");
$client->addScope('
https://www.googleapis.com/auth/analytics.readonly');
$client->setRedirectUri('http://localhost:8080/oauth2callback.php');
$client->setAccessType("offline");
$client->setApprovalPrompt("force");

$access_token = '###';
$refresh_token ='###';

$tokenObj = array('access_token' => $access_token);
$tokenObj = json_encode($tokenObj);
$client->setAccessToken($tokenObj);

if($client->isAccessTokenExpired()) {
printf("Access Token Expired\n");
$client->revokeToken($access_token);
}

$analytics = new Google_Service_Analytics($client);
$accounts =
$analytics->management_accounts->listManagementAccounts();

Re: Web Application using the refresh token/access token

Visitor ✭ ✭ ✭
# 4
Visitor ✭ ✭ ✭
Hi All,

Just as an update, I am now able to generate tokens, it was the google console causing me issues. Now i have a new error:
As I am trying to access analytics data, 1 account is fine, but no more as I get the following error:
(403) User does not have sufficient permissions for this account.
I understand that you can set permissions in GA account with an email- however I have hundreds of accounts/profiles to deal with. Is there a more efficient way to do this? Have searched for an answer and cant find a solid solution to this scenario.

Thanks in advance,
Maurice