Python 如何使用服务帐户为Google日历API构建服务?
我正在使用OAuth2.0和Google服务帐户来尝试和管理日历。下面提供了Python中的示例代码: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
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)
到目前为止,我在运行代码时遇到异常“请求的客户端未授权”
我遵循了谷歌文档中的步骤:
我错过了什么步骤?我的作用域是否仅用于构建服务而不正确?您是否在管理员控制台中添加了该作用域?@SGC您指的是该作用域吗?管理员控制台在“ClientName”下显示服务帐户ClientID,在范围下显示“”。