Google-api-nodejs-client: Error: No access, refresh token or API key is set. setCredentials

Created on 13 Dec 2017  路  13Comments  路  Source: googleapis/google-api-nodejs-client


// Retrieve tokens via token exchange explained above or set them:
oauth2Client.setCredentials({
access_token: 'ACCESS TOKEN HERE',
refresh_token: 'REFRESH TOKEN HERE'
// Optional, provide an expiry_date (milliseconds since the Unix Epoch)
// expiry_date: (new Date()).getTime() + (1000 * 60 * 60 * 24 * 7)
});

this used to work, lately when I updated version, it says
TypeError: oauth2Client.setCredentials is not a function

somewhere in the issues, it was suggested to switch to
oauth2Client.setCredentials = {
access_token: process.env['ACCESS_TOKEN'],
refresh_token: process.env['REFRESH_TOKEN'],
expiry_date: true
};

and now it returns Error: No access, refresh token or API key is set.
How to solve this problem?

Thank you in advance!

Most helpful comment

@piavgh @bantini Here's what worked for me:

    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
    oauth2Client.credentials = {
                    refresh_token: 'your_refresh_token'
    };
    oauth2Client.refreshAccessToken(function(err, tokens){
    console.log(tokens)
    oauth2Client.credentials = {access_token : tokens.access_token}
    callback(oauth2Client);

All 13 comments

@bantini
Nope
oAuth2Client.credentials = credentials does not assign credentials, everything is undefined

@begimai Can you share the code that you are using?

@bantini : Hi, I got the same problem.
Please check it here https://github.com/google/google-api-nodejs-client/issues/869#issuecomment-352401977

Thank you.

I tried to call Google API using request, and it works properly

GoogleTokens.findOne({isObsolete: false}).then((token) => {
                let subscriptionsGetUrl = "https://www.googleapis.com/androidpublisher/v2/applications/" + req.query.packageName + "/purchases/subscriptions/" + req.query.subscriptionId + "/tokens/" + req.query.token + "?access_token=" + token.accessToken;

                request(subscriptionsGetUrl, function (error, response, body) {
                    if (error) {
                        throw error;
                    }

                    res.success(JSON.parse(body).expiryTimeMillis);
                });
            });

@piavgh @bantini Here's what worked for me:

    var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
    oauth2Client.credentials = {
                    refresh_token: 'your_refresh_token'
    };
    oauth2Client.refreshAccessToken(function(err, tokens){
    console.log(tokens)
    oauth2Client.credentials = {access_token : tokens.access_token}
    callback(oauth2Client);

Thank you, everyone
I will try out
My solution was to use "googleapis": "^22.2.0"

Having the same issue with v23.0.0
This should be marked as breaking change. It seems to be due to updates to google/google-auth-library-nodejs

@begimai which google-auth-library version did you use it?

@PabloValor
This one google.auth.OAuth2

Greetings folks! This has been fixed in version 25.0.0. Please do let us know if you're still running into issues!

FWIW, here's the modified (from this) code I use on Cloud Functions which is limited to Node 6.14.0

const {google} = require('googleapis')
const plus = google.plus('v1')
const oauth2Client = new google.auth.OAuth2('your-client-id', 'secret', 'redirect')
google.options({ auth: oauth2Client })
const fromAuthCode = (authCode, userId) => {
  return oauth2Client.getToken(authCode).then(tokenResponse => {
    oauth2Client.setCredentials(tokenResponse.tokens)
    return plus.people.get({ userId: userId }).then(response => {
      return response
    })
  })
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ashishbajaj99 picture ashishbajaj99  路  3Comments

eduDorus picture eduDorus  路  3Comments

lowagner picture lowagner  路  3Comments

ACMerriman picture ACMerriman  路  3Comments

ovaris picture ovaris  路  3Comments