Closed ShivanKaul closed 8 years ago
@ShivanKaul Authorization with discovered_api
method is old style api, just look migration guides
Launchy is not needed on all authorization types. Read this guide too
Yeah, not all the samples have been updated to the new API. Those will be updated soon, but as mentioned in the previous comment, use the migration guide in the mean time to translate the examples from the old API to the current one.
@rusikf @sqrrrl the migration guide does not detail how to upgrade authorization in the new version as detailed on this github issue: https://github.com/google/google-api-ruby-client/issues/291
We cannot figure out how to migrate our authorization from 0.8.x to 0.9.x
@jeremyhaile - create the authorization object your self:
client.authorization = Signet::OAuth2::Client.new(
:authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:client_id => '...',
:client_secret => '...',
:scope => '...',
:redirect_uri => '...'
)
I'll add a note to the migration guide for how to create an signet client as well as adapt other oauth libraries (a few people asked about omniauth/devise, oauth2, etc.)
FWIW, there are some pending changes to google/google-auth-library-ruby aimed at making auth a lot simpler.
@sqrrrl Thanks - that works!
Is there a way to use "API key" authorization? For example, the equivalent of passing "?key=
@jeremyhaile Just set it at the client/service level
client.key = ...
While it's technically valid to set it on a per-request basis, given that in the vast majority of cases people are either using an API entirely with keys or entirely with access tokens, I limited it to the service level to keep the individual method signatures cleaner. That, and it's usually harmless to specify a key and a token together (auth token can be specified per method, unlike keys.) So just set it when you create the service/client and you're good to go
@sqrrrl Thanks - that worked. I did notice that if you don't have a token, you need to leave the authorization as nil to get it to just use the key. Otherwise you can get errors when making api calls using just a "key".
@sqrrrl It would be nice if there was a short example in the README of "Authorization using an API key" "Authorization using OAuth tokens" etc. One example of each would make this much easier to figure out!
The key should be sent for all requests if it's configured, but yes, tokens take precedence over the API key on the back end.
Will update the README as suggested.
@sqrrrl Using the new API I've noticed that it does not appear to be automatically fetching a new access token using the refresh token if authorization fails. Has something changed in that respect or is there something I need to enable automatic refetching?
If I manually call client.authorization.fetch_access_token! it fetches a new token, but this used to be done automatically.
@jeremyhaile Tokens should be getting refreshed. https://github.com/google/google-api-ruby-client/blob/master/spec/google/apis/core/http_command_spec.rb#L30
apply!
is at https://github.com/google/google-auth-library-ruby/blob/master/lib/googleauth/signet.rb
It's possible that there is a small race condition. apply!
is only refreshing if it thinks the token is expired based on the expiration time. It's possible the server thinks it is expired before the client does. If you can confirm if that is the case, it's easy enough to fix.
FYI - added API key info to readme.
Drive API (and a bunch of other related APIs) are now using the new style in their guides & snippets.
As starter code, on the README, we are told to use the Drive module thusly:
But in the Ruby Quickstart Guide for Drive API, the following code is used:
I understand that the instructions in this README say to use
'0.9.pre3'
as opposed to the Quickstart guide. But then I was wondering: how would I do authorization? The version used by the Quickstart guide depends on Launchy for the authorization, but I think that dependency is removed in0.9.pre3
.