如何解决Django在GAE中-OAuth 2.0重定向到本地主机-授权问题
我对python尤其是应用程序部署非常陌生。 我的目标是创建一个使用多个Google API并按计划运行的应用程序。出于测试目的,我创建了一个django应用,该应用只是将具有当前时间戳的行添加到Google表格中。
我为已安装的应用程序创建了Google API凭据,因为我希望此脚本在后台运行。在localhost上运行应用程序时,一切工作正常:
我访问了触发主脚本的localhost路径,并将我重定向到Google身份验证页面。授权后,我将打开“您可以关闭此窗口”页面,并正确添加了测试的Google表格。
但是,当我在App Engine上部署应用程序时,我看到了授权屏幕,但是在确认之后,我被重定向到了localhost,并收到警告:“无法访问此站点,localhost拒绝连接”。
以下是构建Google服务的代码,该代码从主脚本中调用:
def createGoogleCreds():
"""Delete token.pickle manually when adding new services."""
scopes = [
"https://www.googleapis.com/auth/spreadsheets","https://www.googleapis.com/auth/gmail.modify","https://www.googleapis.com/auth/drive"]
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.
print("current",os.getcwd())
if os.path.exists('updateSheet/token.pickle'):
with open('tmp/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(
'tmp/credentials.json',scopes)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('tmp/token.pickle','wb') as token:
pickle.dump(creds,token)
sheetService = build('sheets','v4',credentials=creds,cache_discovery=False)
gmailService = build('gmail','v1',cache_discovery=False)
driveService = build('drive','v3',cache_discovery=False)
return [sheetService,gmailService,driveService]
还有我的凭据.json:
"installed":{
"client_id":"11111-blablalbla.apps.googleusercontent.com","project_id":"bb-bi-project","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":"blablalbla-blablalbla","redirect_uris":["urn:ietf:wg:oauth:2.0:oob:auto","http://localhost"]}}
但是我也尝试从redirect_uris中删除本地主机,但是没有结果。
我的目标是拥有一个使用Google表格作为界面的应用程序,因此我不确定Web应用程序OAuth 2.0是否适合我。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。