0

I've implemented the code to get access_token, refresh_token and expire_date. All these values are stored in the database already.

Now I need a way to get another access_token in case the old one is expired without using google-api-nodejs-client package

The reason is this module recently has bug, you can check it here (my comment is the latest) https://github.com/google/google-api-nodejs-client/issues/869#issuecomment-352401977 https://github.com/google/google-api-nodejs-client/issues/894.

At the moment, I tried something like this

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);
                });
            });

But it will fail when access_token is expired.

Thanks.

0xh8h
  • 3,271
  • 4
  • 34
  • 55
  • You are required to retrieve new access token using no google-api-nodejs-client. If you have a refresh token, you can retrieve the new access token using the refresh token by POST request. You want to need such method. Is my understanding correct? – Tanaike Dec 18 '17 at 22:12
  • @Tanaike: Hi, I mean I need to detect when the access_token is expired, at that time, I can use the refresh_token to request a new one. The problem is, in the official document, there is nowhere I can find how to detect if access_token is expired. – 0xh8h Dec 19 '17 at 01:40
  • Thank you for your reply. I posted an answer. Please confirm it. – Tanaike Dec 19 '17 at 01:59

1 Answers1

2

How about this answer? You can retrieve the expiration time for access token using OAuth2 API. When you want to retrieve it without google-api-nodejs-client, you can use the following method.

Sample script :

var uri = "https://www.googleapis.com/oauth2/v3/tokeninfo?access_token=" + accesstoken;
request.get({uri: uri}, (err, res, body) => {
    console.log(body)
});

Response :

If the access token can be used yet, the following response is returned.

{
    "azp": "#####",
    "aud": "#####",
    "sub": "#####",
    "scope": "#####",
    "exp": "1234567890", // Expiration time
    "expires_in": "3600", // Remaining time
    "access_type": "offline"
}

If the access token has already not been able to used, the following response is returned.

{
 "error_description": "Invalid Value"
}

Reference :

I got above method from following documents.

If this was not useful for you, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Thank you Tanaike. After some research about this, I used this way: Whenever a 401 Unauthorized appears, I catch it and in the catch, use refresh_token to request a new access_token, and call the API again with access_token – 0xh8h Dec 20 '17 at 09:15
  • @Hoang Trinh Thank you for your additional information. – Tanaike Dec 20 '17 at 12:16
  • @Tanaike What about refreshing `idToken`? Please see details [here](https://stackoverflow.com/questions/67975071/google-idtoken-refresh) – Jeb50 Jun 14 '21 at 23:31