问题
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:
- I've enabled the the calendar api in the Google Cloud console
- I've created a new service account, enabled G Suite Domain-wide Delegation, and downloaded the provided key.
- I've added the service account email to the calendar to be able to make changes and create events.
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:
- Create a JSON Web Token (JWT, pronounced, "jot") which includes a header, a claim set, and a signature.
- Request an access token from the Google OAuth 2.0 Authorization Server.
- 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