Google 日历 api 通过 POST 请求进行身份验证

Google Calendar api authentication via POST request

我正在尝试向 google 日历 api 发出 POST 请求,但我不明白如何对其进行身份验证。

我采取了以下步骤来尝试使用服务帐户来执行此操作:

  1. 我在 Google 云控制台
  2. 中启用了日历 api
  3. 我创建了一个新的服务帐户,启用了 G Suite Domain-wide Delegation,并下载了提供的密钥。
  4. 我已将服务帐户电子邮件添加到日历中,以便能够进行更改和创建活动。
  5. 我试图创建一个 POST 请求到 https://www.googleapis.com/calendar/v3/calendars/calendarId/events 的内容 JSON 键作为 Authorization header 的值,但我收到以下错误:

    { "error": { "errors": [ { "domain": "global", "reason": "authError", "message": "Invalid Credentials", "locationType": "header", "location": "Authorization" } ], "code": 401, "message": "Invalid Credentials" } }

我是不是漏掉了一些步骤,或者我没有正确理解身份验证过程? 非常感谢您的帮助。

这不仅仅是将密钥文件的内容应用于您的调用的问题,所有 google api 都需要一个访问令牌才能对其进行身份验证。

要从服务帐户获取访问令牌,您需要执行以下步骤。 Preparing to make an authorized API call

您需要完成一些步骤才能获得访问令牌,您需要调用 api

After you obtain the client ID and private key from the API Console, your application needs to complete the following steps:

  1. Create a JSON Web Token (JWT, pronounced, "jot") which includes a header, a claim set, and a signature.
  2. Request an access token from the Google OAuth 2.0 Authorization Server.
  3. Handle the JSON response that the Authorization Server returns.

我建议您选择自己喜欢的服务器端编程语言并为其寻找客户端库。与您尝试自己使用服务帐户进行身份验证相比,这会让事情变得容易得多。