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

option to work without a logged in user #15

Open philcruz opened 9 years ago

philcruz commented 9 years ago

So I'm working with the Google APIs in a project and came across this package. It's nice that if the call fails it refreshes the token and retries the call for you. However, it's dependent on having a logged in user. You pass options.user and under the hood it fetches/updates from user and user.services.google.

In my project I'm handling OAuth manually so I don't have a logged in user. I would still like to have the refresh-retry functionality though. So I made some modifications that do that. Instead of passing in options.user you would pass options.refreshTokenParams

updateAccessToken = function(access_token) { console.log("in updateAccessToken"); Tokens.upsert( {account: Meteor.settings.account}, { $set: {
accessToken: access_token //expiresIn: result.data.expires_in } }
); }

options = {  
        refreshTokenParams: {
            'client_id': Meteor.settings.public.client_id,
            'client_secret': Meteor.settings.client_secret,
            'refresh_token': tokens.refreshToken,
            'updateTokenFunction': updateAccessToken
        },
        params: { 'access_token': tokens.accessToken  }
        }

    result = GoogleApi.get( apiUrl, options);

Seem like a good approach?

tmeasday commented 9 years ago

Hmm.

So the idea would be to be able to pass in a refreshToken and an updateAccessToken function and have the default implementation be user.services.google.refreshToken and something like Meteor.users.update(user._id, {$set: {'services.google.accessToken': X}}) ?

Is everything happening server-side here for you also? (i.e. we don't publish the refreshToken, call a method to update it).

@zol: what are your thoughts about accepting a PR for this? It adds complexity to the package but it seems a legit use case...

philcruz commented 9 years ago

To clarify, the way I have it working is if you pass in options.user then it works as you have it. Alternatively, if you pass in options.refreshTokenParams like in my example so you can manually manage the tokens. This way it should be backwards compatible.

zol commented 9 years ago

@tmeasday @philcruz - I can see how this would be useful. On the other hand, it's also kind of an edge case given that most do use Meteor accounts. Given that we haven't received requests for this, I would suggest forking the package for now and using it as a submodule in your project. If we receive more requests for the functionality, we'll extend the package.

ganySA commented 9 years ago

I have a very similar requirement. @philcruz did you eventually publish any code, are you using meteor package oauth flow to get the initial tokens? i am looking for something to create the initial request + something to auto refresh

philcruz commented 9 years ago

@ganySA I haven't published my fork yet but I will. I can't do it this week because I'm on travel. I'll do it the following week.

philcruz commented 9 years ago

@ganySA You can check out my code at https://github.com/philcruz/meteor-google-api/tree/develop

phsultan commented 9 years ago

Hi everybody,

I'm using @philcruz package, and I found it very useful to access Google's API (contacts in my case), as the single source of Meteor accounts I have for my users isn't Google.

So I vote for adding this extension if this is something you guys would consider.

lvnr commented 8 years ago

@tmeasday @zol - I develop a dashboard app, where multiple oAuth tokens to multiple services are saved to dashboards, and not to Meteor accounts (since there might be multiple users using the same dashboard). So I assume this is a somewhat generic requirement other people might have and I would really like to see this implemented.