如何解决部署到Docker容器时无法在python中加载Google API日历的token.pickle
我正在尝试致电Google日历api,并检索节日活动。我正在使用api调用google日历,在我的本地计算机上工作正常,但是当我将其托管在docker容器上时不起作用。
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
SCOPES = ['https://www.googleapis.com/auth/contacts.readonly']
def main():
"""
Shows basic usage of the People API.
Prints the name of the first 10 connections.
"""
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:
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.pickle','wb') as token:
pickle.dump(creds,token)
service = build('people','v1',credentials=creds)
我可以在本地但不在容器上加载“ creds = pickle.load(token)”行。为了从容器访问google api,我们需要对容器进行任何设置吗?还是我想念其他东西?
解决方法
flow.run_local_server()
绑定到容器内的 localhost。
使用 --net=host
(或 docker-compose use network_mode:"host"
)启动 docker。
请参阅 https://github.com/googleapis/google-auth-library-python-oauthlib/issues/87#issuecomment-685094486
的完整答案版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。