Google Calendar api authentication via POST request

牧云@^-^@ 提交于 2021-01-29 16:52:47

问题


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. 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:

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

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


回答1:


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.

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

There are steps you will need to complete inorder to get the access token you will need to make a call to the 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.

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.



来源:https://stackoverflow.com/questions/62302708/google-calendar-api-authentication-via-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!