Python 3.x 如何修复重定向uri与Google的重定向uri不匹配
当我试图访问谷歌日历api时,我遇到了这个错误 错误: 错误400:重定向\u uri\u不匹配 请求中的重定向URI,http://localhost:37461/,与为OAuth客户端授权的不匹配。要更新授权重定向URI,请访问: 上面提到的端口Python 3.x 如何修复重定向uri与Google的重定向uri不匹配,python-3.x,google-api,google-oauth,google-calendar-api,Python 3.x,Google Api,Google Oauth,Google Calendar Api,当我试图访问谷歌日历api时,我遇到了这个错误 错误: 错误400:重定向\u uri\u不匹配 请求中的重定向URI,http://localhost:37461/,与为OAuth客户端授权的不匹配。要更新授权重定向URI,请访问: 上面提到的端口http://localhost:37461/总是在变化 这就是我如何设置我的凭证.json { "web": { "client_id": "<id>.apps.google
http://localhost:37461/
总是在变化
这就是我如何设置我的凭证.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"]
}
}
我还在控制台中设置了重定向URL:
有人能帮我吗?实际上,您可以指定要修复的重定向uri的端口,以便将您重定向到正确的url,如果您已经被重定向到http://localhost:37461/
:37461
当您不指定特定端口时,此数字是随机的,正如@Tanaike提到的,您需要将端口修复为3000,如屏幕截图所示。
creds=flow。运行本地服务器(端口=3000)
在您的情况下,如何从http://localhost:3000/create
至http://localhost:3000
脚本将从creds=flow.run\u local\u server(port=0)
修改为creds=flow.run\u local\u server(port=3000)
。顺便说一下,从您的credentials.json
和图像来看,似乎http://localhost
未保存。因此,当您修改重定向URL时,请单击“保存”按钮。请小心这个。如果这个评论对你的情况没有帮助,我道歉。@Tanaike的建议对你的问题有用吗?
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()