【问题标题】:Failing to load token.pickle of google api calendar in python when deployed on to docker container部署到 docker 容器时,无法在 python 中加载 google api 日历的 token.pickle
【发布时间】:2021-01-16 13:52:32
【问题描述】:

我正在尝试调用 google calendar 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)”行。我们是否需要对容器进行任何设置才能从容器访问 google api,或者我还缺少什么?

【问题讨论】:

  • “我可以在线加载”——这是什么意思?您收到错误消息吗?如果是这样,请将其添加到您的问题中。告诉我们究竟是什么不良行为。
  • 您询问有关访问 Google API 的问题,但您的程序试图做的只是从本地磁盘读取文件。问题一定是你没有让token.pickle文件在容器中,在正确的位置,具有正确的权限,以便你的程序找到它并读取它。这确实是该特定代码行可能失败的唯一原因。

标签: python docker google-api-client google-api-python-client docker-container


【解决方案1】:

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查看完整答案

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 2021-10-26
    • 2020-06-19
    • 1970-01-01
    • 2023-04-01
    • 2020-05-19
    • 2020-08-04
    相关资源
    最近更新 更多