【问题标题】:Use the Calendar API without human interaction无需人工交互即可使用日历 API
【发布时间】:2021-12-03 01:22:32
【问题描述】:

我正在尝试从 Google 日历中获取事件,而不向用户提示同意页面。 我为此使用了一个服务帐户(包含所有数据的 json 文件)

但是当我运行脚本时,我得到了这个

from google.oauth2 import service_account
import googleapiclient.discovery
import time
import jwt

SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
SERVICE_ACCOUNT_FILE = '/Users/user/Downloads/XXXXXXXX.json'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = googleapiclient.discovery.build('calendar', 'v3', credentials=credentials)

page_token = None
calendar_list_entry = service.calendarList().get(calendarId='XXXXXXXXXXX@group.calendar.google.com').execute()

Traceback(最近一次调用最后一次): 文件“/Users/user/Downloads/blender.py”,第 16 行,在 calendar_list_entry = service.calendarList().get(calendarId='XXXXXXX@group.calendar.google.com').execute() 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/_helpers.py”,第 131 行,位于 positional_wrapper 返回包装(*args,**kwargs) 文件“/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/googleapiclient/http.py”,第 937 行,在执行 引发 HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: https://www.googleapis.com/calendar/v3/users/me/calendarList/XXXXXXXX%40group.calendar.google.com?alt=json 返回“Not Found”。 详细信息:“[{'domain': 'global', 'reason': 'notFound', 'message': 'Not Found'}]”>`

【问题讨论】:

    标签: python google-calendar-api google-workspace google-api-python-client service-accounts


    【解决方案1】:

    要通过服务帐户使用 Google 日历,您必须已将域范围的委托设置为您的workspace account

    您在请求错误消息时收到 HttpError 404,因为您没有将服务帐户正确委派给您域中有权访问该日历的用户,因此服务帐户无权访问。

    在下面的代码示例中,仔细查看 credentials = credentials.create_delegated(user_email) 行,这是您希望将服务帐户权限委派给的域中的用户

    from googleapiclient.discovery import build
    from oauth2client.service_account import ServiceAccountCredentials
    
    # Email of the Service Account
    SERVICE_ACCOUNT_EMAIL = '<some-id>@developer.gserviceaccount.com'
    
    # Path to the Service Account's Private Key file
    SERVICE_ACCOUNT_PKCS12_FILE_PATH = '/path/to/<public_key_fingerprint>-privatekey.p12'
    
    def create_directory_service(user_email):
        """Build and returns an Calendar service object authorized with the service accounts
        that act on behalf of the given user.
    
        Args:
          user_email: The email of the user. Needs permissions to access the calendar APIs.
        Returns:
          Calendar service object.
        """
    
        credentials = ServiceAccountCredentials.from_p12_keyfile(
            SERVICE_ACCOUNT_EMAIL,
            SERVICE_ACCOUNT_PKCS12_FILE_PATH,
            'notasecret',
            scopes=['https://www.googleapis.com/auth/calendar'])
    
        credentials = credentials.create_delegated(user_email)
    
        return build('calendar', 'v3', credentials=credentials)
    

    Json 密钥文件。有一个使用 json 密钥文件 here 的库示例,它只是不包括您需要添加该行的委托。

    请记住,服务帐户仅适用于 google 工作区域。它不适用于标准的 Google 日历帐户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-09
      • 2017-11-04
      • 2016-10-17
      • 2014-07-17
      • 2018-06-08
      • 2013-08-06
      • 2012-04-02
      • 2016-08-11
      相关资源
      最近更新 更多