Google Calendar JavaScript api, add user to a cale

2019-07-13 17:08发布

I'm developing a site connected with Google Calendar. This site uses Google's JavaScript API to retrieve events of a public calendar and render them on the website. I'm now able to create new events, update events and remove events from my site and be updated with no problem in the Google Calendar.

If I'm able to edit this calendar information is because my Google account has read/write permission on this particular calendar.

My question: is there a way to add users to the list of accounts that have writing permission for this calendar using the JavaScript API? Obviously I don't want to manually add the emails of each account on the calendar settings page.

What can I do? Do I need to make an ACL rule insert (not much documentation on this for JavaScript) or something like that?

1条回答
姐就是有狂的资本
2楼-- · 2019-07-13 18:00

Using Acl: insert you can add a user with "writer" or "owner" permissions, which grant the user read and write permissions to your calendar.

The role assigned to the scope. Possible values are:

  • "none" - Provides no access.
  • "freeBusyReader" - Provides read access to free/busy information. - "reader" - Provides read access to the calendar. Private events will appear to users with reader access, but event details will be hidden.
  • "writer" - Provides read and write access to the calendar. Private events will appear to users with writer access, and event details will be visible.
  • "owner" - Provides ownership of the calendar. This role has all of the permissions of the writer role with the additional ability to see and manipulate ACLs.

Using the API Explorer on that same page I was able to successfully make this request:

POST https://www.googleapis.com/calendar/v3/calendars/12345@group.calendar.google.com/acl?key={YOUR_API_KEY}

{
 "role": "writer",
 "scope": {
  "type": "user",
  "value": "user@example.com"
 }
}

You can use it with the Google API Javascript client like this:

var calendarid = "12345@group.calendar.google.com";
var req = {
  "calendarId": calendarid,
  "resource": {
    "role": "writer",
    "scope": {
      "type": "user",
      "value": "user@example.com"
    }
  }
}
var request = gapi.client.calendar.acl.insert(req);
request.execute(function(resp) {
  console.log(resp);
});
查看更多
登录 后发表回答