我正在尝试调用谷歌日历api并检索假日事件。我正在使用一个对谷歌日历的api调用,在我的本地上运行良好,但当我在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(";在我的本地,但不是在集装箱上。我们需要在容器上进行任何设置才能从容器访问googleapi吗?或者我还缺少什么吗?
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