question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

'Credentials' object has no attribute 'authorize'

See original GitHub issue

I’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:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:29 (11 by maintainers)

github_iconTop GitHub Comments

6reactions
theacodescommented, Aug 16, 2017

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 needs google-auth-httplib2 if you pass it google-auth credentials.

5reactions
theacodescommented, Aug 14, 2017

@Cosbgn when using google-auth with google-api-python-client you only need to specify the credentials to discovery - you do not need to construct your own http instance. So:

v3 = build('analytics', 'v3', credentials=credentials)
try:
    account_summaries = v3.management().accountSummaries().list().execute()
except Exception as error:
    return error_page(request, error)
google_email = account_summaries['username']

Should work.

Read more comments on GitHub >

github_iconTop Results From Across the Web

google api dict has no attribute authorize - Stack Overflow
When you want to use service = build('sheets', 'v4', credentials=creedentials) with the service account and creedentials={has the ...
Read more >
AttributeError: 'Credentials' object has no attribute 'authorize'
I am getting following error while deploying the app. I have changed peoject Id in config file. ERROR: (gcloud.app.deploy) Error Response: ...
Read more >
Python quickstart | People API - Google Developers
Authorize credentials for a desktop application · In the Google Cloud console, go to Menu menu > APIs & Services > Credentials. ·...
Read more >
oauth2client.client module - Read the Docs
Credentials can be applied to an httplib2.Http object using the authorize() method, which then adds the OAuth 2.0 access token to each request....
Read more >
OAuth2Token' object has no attribute 'authorize' even with ...
You're using flow.fetch_token incorrectly. It's return value is not a credentials object, but a set of tokens. The documentation explains that you shouldn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found