percolatestudio / meteor-google-api

A simple API encapsulating some common patterns regarding Google's APIs
https://atmospherejs.com/percolate/google-api
MIT License
48 stars 30 forks source link

offline access / refresh tokens not working? #39

Closed quape closed 7 years ago

quape commented 7 years ago

Hi there,

I'm calling GoogleApi.get('calendar/v3/calendars/primary/events', { user: user, ... on a user who is not logged in to fetch calendar events. The user was previously logged in to Google with forceApprovalPrompt: true, requestOfflineToken: true.

Problem is that calling API results with undefined after a period of time so I guess there is something from with the refreshing of tokens? What could be the problem?

Thanks

quape commented 7 years ago

I realized that when I refresh the page of this user (so that he is online and logged in again) the API call also works again. So my problem might have something to do with the handling of users itself, I still have no idea.

quape commented 7 years ago

Solved. Problem was that refreshToken is not renewed and saved into user collection after expired. I did it manually using this:

        var res = HTTP.post(
            "https://www.googleapis.com/oauth2/v3/token", {
                params: {
                    'client_id': config.clientId,
                    'client_secret': config.clientSecret,
                    'refresh_token': user.services.google.refreshToken,
                    'grant_type': 'refresh_token'
                }
            })

        if(res.data.access_token) {
            Meteor.users.update(user._id, { $set: {
                'services.google.accessToken': res.data.access_token,
                'services.google.expiresAt': +new Date() + res.data.expires_in * 1000
            }})