Python 如何使用服务帐户为Google日历API构建服务?

Python 如何使用服务帐户为Google日历API构建服务?,python,google-api,google-calendar-api,Python,Google Api,Google Calendar Api,我正在使用OAuth2.0和Google服务帐户来尝试和管理日历。下面提供了Python中的示例代码: from oauth2client.client import SignedJwtAssertionCredentials from httplib2 import Http from apiclient.discovery import build client_email = "<service_account_client_email>" with open("key.p1

我正在使用OAuth2.0和Google服务帐户来尝试和管理日历。下面提供了Python中的示例代码:

from oauth2client.client import SignedJwtAssertionCredentials
from httplib2 import Http
from apiclient.discovery import build

client_email = "<service_account_client_email>"
with open("key.p12") as f:
    priv_key = f.read()

cred = SignedJwtAssertionCredentials(client_email, priv_key, "https://www.googleapis.com/auth/calendar", sub="<user_to_impersonate>")
http_auth = cred.authorize(Http())

service = build(serviceName='calendar', version='v3', http=http_auth)
从oauth2client.client导入SignedJwtAssertionCredentials
从httplib2导入Http
从apiclient.discovery导入生成
客户_电子邮件=“”
打开(“键p12”)作为f:
priv_key=f.read()
cred=SignedJwtAssertionCredentials(客户端电子邮件,私钥)https://www.googleapis.com/auth/calendar“,sub=”“)
http_auth=cred.authorize(http())
service=build(serviceName='calendar',version='v3',http=http\u auth)
到目前为止,我在运行代码时遇到异常“请求的客户端未授权”

我遵循了谷歌文档中的步骤:

  • 将域范围的权限委托给您的服务帐户(我创建了服务帐户,但我不是域的管理员。但是,管理员用户执行了此步骤)
  • 已验证作用域设置为使用日历,并且API已在开发人员控制台中启用
  • 与服务帐户共享日历(尽管管理日历的选项不可用)
    我错过了什么步骤?我的作用域是否仅用于构建服务而不正确?

    您是否在管理员控制台中添加了该作用域?@SGC您指的是该作用域吗?管理员控制台在“ClientName”下显示服务帐户ClientID,在范围下显示“”。