本文介绍了通过POST请求进行Google Calendar api身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to issue a POST request towards the google calendar api, but I fail to understand how to authenticate it.

我采取了以下步骤尝试并使用服务帐户来这样做:

I took the following steps to try and use a service account to do so:

  1. 我已在Google Cloud控制台中启用了日历api
  2. 我创建了一个新的服务帐户,启用了 G Suite域范围委派,并下载了提供的密钥.
  3. 我已将服务帐户电子邮件添加到日历中,以便能够进行更改和创建事件.
  4. 我已尝试创建对 https://www的POST请求.googleapis.com/calendar/v3/calendars/ calendarId /events,其中JSON密钥的内容作为 Authorization 标头的值,但我收到了以下错误:

  1. I've enabled the the calendar api in the Google Cloud console
  2. I've created a new service account, enabled G Suite Domain-wide Delegation, and downloaded the provided key.
  3. I've added the service account email to the calendar to be able to make changes and create events.
  4. I've tried to create a POST request to https://www.googleapis.com/calendar/v3/calendars/calendarId/events with the contents of the JSON key as the value of the Authorization header, but I receive the following error:

{错误": {错误":[{"domain":"global","reason":"authError","message":无效的凭证","locationType":"header",位置":授权"}],代码":401,"message":无效的凭据"}}

我是否缺少某些步骤,或者我不正确理解身份验证过程?非常感谢您的帮助.

Am I missing some steps or have I do not understand correctly the authentication process?I would appreciate your help.

推荐答案

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

Its not simply a matter of applying the contents of the key file to your call, all google apis need an access token in order to authenticate them.

要从服务帐户获取访问令牌,您需要执行以下步骤.准备进行授权的API调用

To get an access token from a service account you need to follow the following steps. Preparing to make an authorized API call

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

There are steps you will need to complete inorder to get the access token you will need to make a call to the api

  1. 创建一个JSON Web令牌(JWT,发音为"jot"),其中包括标头,声明集和签名.
  2. 从Google OAuth 2.0授权服务器请求访问令牌.
  3. 处理授权服务器返回的JSON响应.

我建议您选择自己喜欢的服务器端编程语言并为其找到客户端库.这样一来,您便可以尝试轻松地使用服务帐户进行身份验证了.

I recommend you pick your favorite server sided programming language and find a client library for it. It will make things much easier then you trying to authncate using a service account by yourself.

这篇关于通过POST请求进行Google Calendar api身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:19
查看更多