0xh8h
0xh8h

Reputation: 3509

How to handle Google API access_token expired without using google-api-nodejs-client?

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.

Upvotes: 0

Views: 1127

Answers (1)

Tanaike
Tanaike

Reputation: 201378

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.

Upvotes: 2

Related Questions