python-3.xgmail-api

gmail api keeps changing the localhost address (python)


i followed the instructions to use a gmail oauth api in my application.

registering the url at Authorized redirect URIs doesnt seem to work as the number after localhost seems to change in every run (please see the image)

Problem is, when Google’s OAuth2 page is called, redirect_uri keeps getting set to http://localhost:/

image of the error

this error comes when i run the code from https://developers.google.com/gmail/api/quickstart/python


Solution

  • In line 44 of the quickstart.py script, assign to the server the same port that you will use in the credentials.

    Then change the statement to be

    creds = flow.run_local_server(port=<your-credentials-port>)
    

    Use a different port than your running application. It can't be a port in use (ie 5000 or 8000). Any port not in use will do (ie 5005 or 8008). This is just for the flow.

    See google_auth_oauthlib.flow module.