I am unable to access my Google sheet, here is the error I get:
Error 400: redirect_uri_mismatch
You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy.
If you're the app developer, register the redirect URI in the Google Cloud Console.
Request details: redirect_uri=http://localhost:57592/
Below I'll share my code, relevant parts of the credentails file as well as my Google Cloud console configurations I have in place. Just to note, From my inexperienced eyes (as far as python and google console is concerned), it seems as if I've checked the right boxes based on what I've found on StackOverflow.
PYTHON:
from __future__ import print_function
from datetime import datetime
import pickle
import os.path
import pyodbc
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# connecting to the database
connection_string = 'DRIVER={driver};PORT=1433;SERVER={server};DATABASE={database};UID={username};PWD={password}; MARS_Connection={con}'.format(driver= 'ODBC Driver 17 for SQL Server', server= server, database= database, username=username, password=password, con='Yes')
conn = pyodbc.connect(connection_string)
print("Connected to the database successfully")
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']
# The ID and range of a sample spreadsheet.
# https://docs.google.com/spreadsheets/d/spreadsheetId/edit#gid=0
SPREADSHEET_ID = spreadSheetID
RANGE_NAME = 'CAPS!A2:K'
#def dataUpload():
def dataUpload():
creds = None
# The file token.pickle 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.pickle'):
with open('token.pickle', '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:
print ("we going here")
flow = InstalledAppFlow.from_client_secrets_file(
'ApplicantList/credentials.json', SCOPES)
flow.redirect_uri = 'http://127.0.0.1:8000' #added this
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
service = build('sheets', 'v4', credentials=creds)
In google console I have both a Service account and an OAUTH 2.0 ClientID.
I have the following URI's registered there:
Each time the error comes up, I add the URI to the redirect list but then I get the same error with a new URI port (i.e http://localhost:[new port]).
And here is my credentails file:
{"web":{"client_id":"xxxxx","project_id":"xxxx","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","redirect_uris":["http://127.0.0.1:8000"],"javascript_origins":["http://127.0.0.1:8000"]}}
What am I doing wrong? Thanks!
CodePudding user response:
Authorized redirects URIs
Just making sure this question is not left unanswered, after confirmation from the comments that the suggestion on how to fix it was successful, would be providing insight on the issue and the fix.
Due to each call being made changes the local host port, adding them manually each time it is called would always result in an error. Editing the URI to http://localhost/
would allow it to run with no problem even if the port is changed:
References
- Discussion thread with details and references with similar issues: Google OAuth 2 authorization - Error: redirect_uri_mismatch