Reputation: 3873
I am getting this error when I try to access the google calendar api
error:
Error 400: redirect_uri_mismatch The redirect URI in the request, http://localhost:37461/, 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}
that port mentioned above http://localhost:37461/
always changes.
And this is how I have set my credentials.json
{
"web": {
"client_id": "<id>.apps.googleusercontent.com",
"project_id": "stunning-surge-291419",
"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": ["http://localhost:3000/create"],
"javascript_origins": ["http://localhost:3000"]
}
}
And this is the python
I am using to list the events in:
import datetime
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/calendar']
CREDENTIALS_FILE = "credentials.json"
def get_calendar_service():
creds = None
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:
flow = InstalledAppFlow.from_client_secrets_file(
CREDENTIALS_FILE, SCOPES)
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('calendar', 'v3', credentials=creds)
return service
from createevent import get_calendar_service
def main():
service = get_calendar_service()
# Call the Calendar API
print('Getting list of calendars')
calendars_result = service.calendarList().list().execute()
calendars = calendars_result.get('items', [])
if not calendars:
print('No calendars found.')
for calendar in calendars:
summary = calendar['summary']
id = calendar['id']
primary = "Primary" if calendar.get('primary') else ""
print("%s\t%s\t%s" % (summary, id, primary))
if __name__ == '__main__':
main()
And I have set the redirect urls in the console as well:
Can someone please help me?
Upvotes: 3
Views: 3117
Reputation: 571
actually you can specify the port for the redirection uri to be fixed so that you will be redirected to the right url, in your case you have been redirected to http://localhost:37461/ :37461
this number is randomized when you don't specify a specific port, as @Tanaike mentions you will need to fix the port to 3000 as shown in your screenshot.
creds = flow.run_local_server(port=3000)
Upvotes: 1