Google Calendar API - 日历上的404(服务帐户)

时间:2014-01-28 14:16:20

标签: python calendar google-api google-calendar-api

我的简单日历API包装器出了问题。

我的代码:

key_file = open('api_key.p12', 'rb')
key = key_file.read()
key_file.close()

credentials = SignedJwtAssertionCredentials(
 config.SERVICE_ACCOUNT_EMAIL,
 key,
 scope='https://www.googleapis.com/auth/calendar'
)
http = httplib2.Http()
http = credentials.authorize(http)

service = build("calendar", "v3", http=http)

我称之为:

service.events().insert(calendarId=calendar_id,
                               body=event_body).execute()

我总是

HttpError: <HttpError 404 when requesting   https://www.googleapis.com/calendar/v3/calendars/my_company_resource_id@resource.calendar.google.com?alt=json returned "Not Found">

当我使用“普通”OAuth2范围(本地Web浏览器中的授权)时,它可以工作,但我想在无头服务器上部署我的应用程序,因此我必须使用服务帐户。 我在google apps管理面板中授予了日历api的权限,但它不起作用。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您还必须提供有权访问此日历的用户电子邮件。见下面的例子:

  credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
      scope='https://www.googleapis.com/auth/calendar', sub=user_email)

如何创建您可以在此页面上找到的凭据对象的好示例:Instantiate a Drive service object。 (Python选项卡)。它显示了Drive API的示例,但您也可以将其用于Calendar API。