部署到docker容器上时,无法在python中加载google api日历的token.pickle
我正在尝试调用GoogleCalendarAPI并检索假日事件。我正在使用一个对google calendar的api调用,在本地运行良好,但当我在docker容器上托管它时,它就不起作用了部署到docker容器上时,无法在python中加载google api日历的token.pickle,python,docker,google-api-client,google-api-python-client,docker-container,Python,Docker,Google Api Client,Google Api Python Client,Docker Container,我正在尝试调用GoogleCalendarAPI并检索假日事件。我正在使用一个对google calendar的api调用,在本地运行良好,但当我在docker容器上托管它时,它就不起作用了 from __future__ import print_function import pickle import os.path from googleapiclient.discovery import build from google_auth_oauthl
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\u local\u server()
绑定到容器中的localhost
使用--net=host
启动docker(或docker compose使用网络模式:“主机”
)
请参阅流中的完整答案。运行本地服务器()已绑定到容器内的本地主机 使用
--net=host
启动docker(或docker compose使用网络模式:“主机”
)
请参见中的完整答案“我可以在线加载”-这是什么意思?您是否收到错误消息?如果是,请将其添加到您的问题中。告诉我们到底是什么不好的行为。你问关于访问谷歌API,但你的程序试图做的只是从本地磁盘读取一个文件。问题一定是您没有使容器中的
token.pickle
文件在正确的位置具有正确的权限,以便您的程序找到并读取它。这确实是特定代码行可能失败的唯一原因。“我能够在该行中加载”-这意味着什么?您是否收到错误消息?如果是,请将其添加到您的问题中。告诉我们到底是什么不好的行为。你问关于访问谷歌API,但你的程序试图做的只是从本地磁盘读取一个文件。问题一定是您没有使容器中的token.pickle
文件在正确的位置具有正确的权限,以便您的程序找到并读取它。这确实是特定代码行失败的唯一原因。