0

I would like to regenerate a token while having no access to a browser on a server side environment.

I am having difficulties generating a new token when the current token expires during the Oauth2.0 authorization with the Google Drive API.

I have got to a point where my code provides a URL which we would need to GET to get access to a code that needs to be filled back on the server.

I have tried looking around on StackOverflow but have been unsuccessful in finding a solution. Given below is the current state of my code:

def get_latest_token():
    store = file.Storage('token.json')
    creds = store.get()
    token_params = load_json_as_dict('token.json')
    if not creds or creds.access_token_expired:
        flow = client.flow_from_clientsecrets('credentials.json', 'https://www.googleapis.com/auth/drive')                                                                                                                                                                                                                           
        flags = tools.argparser.parse_args(args=[])
        creds = tools.run_flow(flow, store, flags)
    bearer_token = "Bearer " + load_json_as_dict('token.json')["access_token"]                                                                                                                                                                                                                                        
    return bearer_token

Any help appreciated.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
lordlabakdas
  • 1,163
  • 5
  • 18
  • 33
  • I do not see in your code where you are refreshing the token. Your code is for authentication / authorization. Review this link for refreshing tokens: https://developers.google.com/identity/protocols/OAuth2WebServer#offline – John Hanley Dec 05 '18 at 21:52
  • 1. Do you personally have access to this drive account. If so you sould be using a service account. 2. If not one you should be requesting offline access then you will have a refresh token which you can use to request a new access token when the access token expires – Linda Lawton - DaImTo Dec 06 '18 at 08:20
  • I do have access to this drive account – lordlabakdas Dec 06 '18 at 08:21
  • Then you should look into this https://developers.google.com/api-client-library/python/auth/service-accounts – Linda Lawton - DaImTo Dec 06 '18 at 11:10
  • https://stackoverflow.com/questions/19766912/how-do-i-authorise-an-app-web-or-installed-without-user-intervention/19766913#19766913 – pinoyyid Dec 06 '18 at 20:02

0 Answers0