I try to auth through google with python, but i get 400 error (redirect URI mismatch)
but in google console I set another redirect uri
my credentials.json
also has another redirection uri
{
"web": {
"client_id": "...",
"project_id": "resume-hub",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_secret": "...",
"redirect_uris": [
"https://example.com/"
]
}
}
I don't understand why generated wrong url to authorize:
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=19********8-m5k0j08t3u36bsed38kv60qmjpaj9pn3.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A34161%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdocuments.readonly&state=q5LBokF7CxHKJH67TsxLni2b0VaSQ6&access_type=offline
my code:
from __future__ import print_function
import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/documents.readonly']
# The ID of a sample document.
DOCUMENT_ID = '13GMqNrDu604Cd8U_N5E6gPkL9rSJZSgRSDZHtg16Kao'
def test():
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# 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:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
try:
service = build('docs', 'v1', credentials=creds)
# Retrieve the documents contents from the Docs service.
document = service.documents().get(documentId=DOCUMENT_ID).execute()
print('The title of the document is: {}'.format(document.get('title')))
except HttpError as err:
print(err)
the library automatically creates the redirect URI based upon your application and how its running your code is running on local host as http so it's sending from localhost http
you are getting redirect mismatched because you haven't added that as a valid redirect URI in cloud console.
Tbh your code is designed for a installed app you should have created native client not a web app client then you wouldn't have this issue