Python 脚本在更新以处理 SignedJwtAssertionCredentials 删除后在 Google 日历中插入事件时出现 404 错误
Python script getting 404 error when inserting event in a Google Calendar after being updated to deal with removal of SignedJwtAssertionCredentials
Google oauth2client 库更改了凭据的管理方式,将 SignedJwtAssertionCredentials 替换为 oauth2client.service_account.ServiceAccountCredentials。 https://github.com/google/oauth2client/issues/401 中对此进行了介绍,我已按照 dhermes 提供的步骤使用带有新代码的 PEM 文件进行身份验证。
阅读https://developers.google.com/identity/protocols/OAuth2ServiceAccount,其中提到使用:
delegated_credentials = credentials.create_delegated('user@example.org')
我也将其添加到我的代码中,但我不知道在哪里使用 delegated_credentials 并且文档没有提供示例。
创建日历事件的脚本的主要部分是:
created_event = service.events().insert(calendarId=CALENDAR, body=event).execute()
我在那里遇到了 404 错误。但是,如果我使用 Google 的 API 测试器和 copy/paste 相同的日历 ID 以及我的脚本生成的 JSON,则 API 有效。
全部 我在我的脚本中更改了身份验证代码,但我没有收到身份验证错误响应。该脚本在 Google 的 API 更改之前工作,我正在使用 Google 的 Python 库与他们的 API.[=16 交互=]
有什么建议可以解决这个问题吗?
事实证明,解决方案与服务本身的创建方式有关。
在脚本中,"service" 是这样创建的:
service = build("calendar", "v3", http=http)
需要更改的是 "http" 的定义方式。我需要(重新)介绍委托凭证:
http = httplib2.Http()
http = delegated_credentials.authorize(http)
之后,一切正常。
Google oauth2client 库更改了凭据的管理方式,将 SignedJwtAssertionCredentials 替换为 oauth2client.service_account.ServiceAccountCredentials。 https://github.com/google/oauth2client/issues/401 中对此进行了介绍,我已按照 dhermes 提供的步骤使用带有新代码的 PEM 文件进行身份验证。
阅读https://developers.google.com/identity/protocols/OAuth2ServiceAccount,其中提到使用:
delegated_credentials = credentials.create_delegated('user@example.org')
我也将其添加到我的代码中,但我不知道在哪里使用 delegated_credentials 并且文档没有提供示例。
创建日历事件的脚本的主要部分是:
created_event = service.events().insert(calendarId=CALENDAR, body=event).execute()
我在那里遇到了 404 错误。但是,如果我使用 Google 的 API 测试器和 copy/paste 相同的日历 ID 以及我的脚本生成的 JSON,则 API 有效。
全部 我在我的脚本中更改了身份验证代码,但我没有收到身份验证错误响应。该脚本在 Google 的 API 更改之前工作,我正在使用 Google 的 Python 库与他们的 API.[=16 交互=]
有什么建议可以解决这个问题吗?
事实证明,解决方案与服务本身的创建方式有关。
在脚本中,"service" 是这样创建的:
service = build("calendar", "v3", http=http)
需要更改的是 "http" 的定义方式。我需要(重新)介绍委托凭证:
http = httplib2.Http()
http = delegated_credentials.authorize(http)
之后,一切正常。