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

Error: ... Use this.userId in publish functions #10

Open jiku opened 9 years ago

jiku commented 9 years ago

With

channelById: (id) ->
      try
        GoogleApi.get "/youtube/v3/search", { params: { part: "snippet", channelId: id }}, (err, data) ->
      catch err
        console.log err

I get

err [Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.]

When using GoogleApi in a package. Any ideas? Tried with a clean 1.0.5 app and package X. Only dependencies in package X are what's needed to log in to Google, which it does just fine. Tests run fine too. I'm using the same credentials successfully in other apps, so I'm pretty sure it's not a permissions, scope, etc, problem.

tmeasday commented 9 years ago

Hi @jiku - The package assumes you are running in a method call (so it knows which user's credentials to use when talking to google).

Can you try GoogleApi.get(..., {user: Meteor.users.findOne(this.userId)}) or the like? Does that fix the problem for you?

I don't think there's any way that from inside a library we can detect that we are running in the context of a publish function, so we'll need you to pass the user in no matter what. We should document this better I think.

jiku commented 9 years ago

Thanks, that fixed it. =) I've added if (options.user) delete options.user; before the API call in google-api-sync.js, but maybe that's something Meteor strips off before the call is made?

tmeasday commented 9 years ago

I'm not sure what you mean @jiku ?

jiku commented 9 years ago

Sorry, should've added some context

...
if (options.user) delete options.user; // Avoid sending user? Or will Meteor discard extra HTTP.call options being passed to the API?
HTTP.call(method, this._host + '/' + path, options, function(error, result) {
...

I'm not that proficient with Meteor/Node yet, so it's a bit unclear to me if this became an issue with https://github.com/meteor/meteor/issues/1703

tmeasday commented 9 years ago

Good pickup @jiku. It probably doesn't do anything as request and HTTP don't have a user option AFAIK, but we shouldn't pass it through. #11

jiku commented 9 years ago

Cool. :) I made a PR if that's useable.

ghost commented 9 years ago

Is this issue supposed to be closed @jiku? I'm getting the [Error: Meteor.userId can only be invoked in method calls. Use this.userId in publish functions.] error in my publish-functions, and when I try to implement your code I get a 500 error regarding refreshtokens.