Search code examples
google-oauthcredentialsgoogle-api-python-clientservice-accounts

How to get access token from instance of google.oauth2.service_account.Credentials object?


How to get access token from instance of google.oauth2.service_account.Credentials object?

I need to access an google api endpoint which needs authorisation. For that, I need to provide an access token in request headers.

How to get the access token from this instance of Credentials object?

In case of oauth2client.service_account.ServiceAccountCredentials object, they provide a method to fetch an access token(called get_access_token()), but I could not find any such method for google.oauth2.service_account.Credentials. Any help would be appreciated.

I want to do this inside python script.


Solution

  • Im not sure why you need one as you should be building a service object for which ever api it is you are trying to access. However the following should give you the access token.

    credentials = ServiceAccountCredentials.from_json_keyfile_name(KEY_FILE_LOCATION, SCOPES) 
    
    credentials.access_token