5

I am using google api nodejs , I try to get data from google Anaytics

var google = require('googleapis');
var OAuth2Client = google.auth.OAuth2;
var CLIENT_ID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
var CLIENT_SECRET = 'xxxxxxxxxxxxxxxxxxxxxx';
//var REDIRECT_URL = 'http://yieldops.co/oauth2Callback';
var REDIRECT_URL = 'http://localhost:8000/oauth2Callback';
var oauth2Client = new OAuth2Client(CLIENT_ID, CLIENT_SECRET,REDIRECT_URL);

var credentials={}
credentials.expiry_date= "1463514280558",
credentials['refresh_token']="aaaaaaaaa"
credentials['access_token']="aaaaaaaaaaaaaaa"
oauth2Client.setCredentials(credentials)

var gauth = {
'auth': outh2Client,
'ids': 'ga:101' ,
'start-date': '2016-01-01',
'end-date': 'today',
'metrics': 'ga:impressions,ga:sessions,ga:pageviews',
'dimensions': 'ga:date'
}

analytics.data.ga.get(gauth, function (err, gaData) {
console.log("err 1234",err)
console.log("gaData ",gaData)
//console.log("ga",Object.keys(gaData))
})

Note Now the problem is if access token is not expire then it give me data , and if access token is expire then it gave me 400 error Invalid grant . And if I remove expiry_date from credentials then it gave me error

{ [Error: Invalid Credentials]
code: 401,
errors: 
[ { domain: 'global',
reason: 'authError',
message: 'Invalid Credentials',
locationType: 'header',
location: 'Authorization' } ] }
abhaygarg12493
  • 1,565
  • 2
  • 20
  • 40

2 Answers2

5

access_token have expire time is 1 hour. You must refresh access_token when it's expired.

oauth2Client.refreshAccessToken(function(err, tokens) {
  // your access_token is now refreshed and stored in oauth2Client
  // store these new tokens in a safe place (e.g. database)
});

Can you find it here

TheLittleHawk
  • 628
  • 3
  • 22
KibGzr
  • 2,053
  • 14
  • 15
  • 1
    They removed the docs from README.md but [here's a link to the old one](https://github.com/googleapis/google-api-nodejs-client/blob/c00d1892fe70d7ebf934bcebe3e8a5036c62440c/README.md). I'd provide a link to the implementation, but I wasn't able to find it. – greeble31 Sep 11 '18 at 20:04
  • Looks like [`refreshAccessToken` has been deprecated](https://github.com/googleapis/google-auth-library-nodejs/blob/fbad5d383be691099a7dab29dca27d5263c4dfb1/src/auth/oauth2client.ts#L697). The documentation recommends to use `getRequestHeaders` instead. – Frenchcooc Feb 26 '21 at 20:26
4

No need to explicitly refresh token, when access_token expired. Oauth2 Client will refresh token automatically and the request is replayed, provided when you get refresh_token in first authorization, set this refresh_token in OAuth2 client credentials as shown in the following code

const {tokens} = await oauth2Client.getToken(code)
oauth2Client.setCredentials(tokens);

Still, you want to manually refresh the access_token, then use the following code.

oauth2Client.on('tokens', (tokens) => {
  if (tokens.refresh_token) {
    // store the refresh_token in my database!
    console.log(tokens.refresh_token);
  }
});

OR

oauth2Client.refreshAccessToken((err, tokens) => {
  // your access_token is now refreshed and stored in oauth2Client
  // store these new tokens in a safe place (e.g. database)
});

References:

  1. https://github.com/googleapis/google-api-nodejs-client/blob/c00d1892fe70d7ebf934bcebe3e8a5036c62440c/README.md#making-authenticated-requests
  2. https://github.com/googleapis/google-api-nodejs-client/blob/c00d1892fe70d7ebf934bcebe3e8a5036c62440c/README.md#manually-refreshing-access-token
  3. https://github.com/googleapis/google-api-nodejs-client#handling-refresh-tokens
Pankaj Shinde
  • 3,361
  • 2
  • 35
  • 44