0

Environment details

  • OS type and version: Ubuntu 18.10
  • Python version: Python 3.8.6
  • pip version: pip 9.0.2 from python 3.8
  • google-api-python-client version: 1.12.3
Steps to reproduce
  1. Copy the code from People API example
  2. Use the code below to get credentials
  3. Deploy your app to Heroku
  4. Login into Google Console. Register you app. Setup redirect url so that it matches the url of your Heroku application. I added it with https://.... Beside this, I also added http://localhost. Both url were withouth trailing slashes and withouth port numbers
  5. Run the app. It will produce the error
Code example
def get_creds(SCOPES):
    creds = None
    cred_dir = os.path.join(BASE_DIR, 'credentials.json')
    pickle_dir = os.path.join(BASE_DIR, 'token.pickle')

    # The file token.pickle is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists(pickle_dir):
        with open(pickle_dir, 'rb') as token:
            creds = pickle.load(token)

    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            config = json.loads(os.environ['CRED'])
            flow = InstalledAppFlow.from_client_config(config, SCOPES)
            flow.redirect_uri = os.environ['REDIRECT_URL']
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open(pickle_dir, 'wb') as token:
            pickle.dump(creds, token)
    return creds

Use it like this:

    creds = get_creds(['https://www.googleapis.com/auth/contacts'])

    service = build('people', 'v1', credentials=creds)

REDIRECT_URL is set to Heroku application url, starting with https://, withouth trailing slash. I also tested it in Python console:

>>> import os
>>> os.environ['REDIRECT_URL']
Stack trace

Browser output (there is no console stack trace):

400: redirect_uri_mismatch
The redirect URI in the request, http://localhost:33779/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

I think that the line:

flow.redirect_uri = os.environ['REDIRECT_URL']

does not actually change

What have I tried:

  • this, I double checked for correct urls. I also added urls with https and with trailing slashes
Matej J
  • 615
  • 1
  • 9
  • 33

1 Answers1

0

I find similar problem with an accepted answer form here

In following line: creds = flow.run_local_server(port=0)

try port 8080 instead of 0 and check the outcome.

Pejvak
  • 150
  • 7
  • This does not work. Heroku does not expose any ports, so it wont make any difference – Matej J Oct 08 '20 at 07:15
  • Heroku exposes random (semi?) ports for your apps. But you can access them through the environment variable $PORT – Shuhari Aug 21 '21 at 15:45