'Credentials' object has no attribute 'authorize'
See original GitHub issueI’m migrating my app from the depriciated oAuth2Client to this library.
I use requests_oauthlib
to get the refresh_token
, the access_token
and all the rest.
Then I build the credential
object like this:
credentials = google.oauth2.credentials.Credentials(
access_token,
refresh_token = refresh_token,
token_uri = 'https://accounts.google.com/o/oauth2/token',
client_id = settings.GOOGLE_CLIENT_ID,
client_secret = settings.GOOGLE_CLIENT_SECRET
)
I’m not sure about the token_uri
, what am I supposed to put there?
In any case this seems to work, making a simple call like this one:
authed_session = AuthorizedSession(credentials)
response = authed_session.get('https://www.googleapis.com/oauth2/v1/userinfo')
works as expected and I do get back the results. However my app relies a lot on the Google Analytic api, which uses its own build method.
With oAuth2client I used to do (to get properties using v3):
http = httplib2.Http()
http = credentials.authorize(http)
v3 = build('analytics', 'v3', http=http)
try:
account_summaries = v3.management().accountSummaries().list().execute()
except Exception as error:
return error_page(request, error)
google_email = account_summaries['username']
And to get metrics with v4:
http = credentials.authorize(httplib2.Http())
DISCOVERY_URI = ('https://analyticsreporting.googleapis.com/$discovery/rest')
analytics = build('analytics', 'v4', http=http, discoveryServiceUrl=DISCOVERY_URI)
and then: analytics.reports().batchGet( ...........etc)
However now that I migrate to this library I don’t know how I can use the build method. All the documentation is really old and still referencing the depreciated library. How can I authenticate and use correctly google-auth with the Google Analytics API?
Thanks
Issue Analytics
- State:
- Created 6 years ago
- Reactions:3
- Comments:29 (11 by maintainers)
Hey hey, progress! 😃
Let me know if I can do anything to help from here. I think I’m going to add a better error message to
googleapiclient
to say that it needsgoogle-auth-httplib2
if you pass itgoogle-auth
credentials.@Cosbgn when using
google-auth
withgoogle-api-python-client
you only need to specify the credentials todiscovery
- you do not need to construct your own http instance. So:Should work.