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
- Copy the code from People API example
- Use the code below to get credentials
- Deploy your app to Heroku
- 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 addedhttp://localhost
. Both url were withouth trailing slashes and withouth port numbers - Run the app. It will produce the error
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