问题
I'm using the Google Calendar API in my Rails 5.2.3 app and trying to create events on a Google Calendar within my G Suite domain. I'm already able to read the calendar and list events. When I try to insert an event, I get the error "requiredAccessLevel: You need to have writer access to this calendar."
As this is for an internal application, I have registered a service account in the Google API console and have verified that the account is able to read calendars and events.
In the G Suite Admin Console, I've given the service account permissions for the scopes https://www.googleapis.com/auth/calendar and https://www.googleapis.com/auth/calendar.events
I created the calendar itself under my account and have added the service account with "Make changes to events" permissions
This is a sample of the code I'm running.
calendar_id = '12345' #obviously not but I've verified that I have the right calendar.
scopes = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events']
authorization = Google::Auth.get_application_default(scopes)
client = Google::Apis::CalendarV3::CalendarService.new
client.authorization = authorization
start_time = Time.current
end_time = Time.current + 2.hours
newEvent = Google::Apis::CalendarV3::Event.new({
summary: "Sample Event",
start: {
date_time: start_time.rfc3339,
time_zone: start_time.strftime("%Z")
},
end: {
date_time: end_time.rfc3339
time_zone: end_time.strftime("%Z")
}
})
result = client.insert_event(calendar_id, newEvent)
I'm sure I've missed a permission somewhere for write access, but haven't figured out why I get "requiredAccessLevel: You need to have writer access to this calendar." instead of the insert working. Thanks for any suggestions!
回答1:
Thanks to Chloe in the comment above. The solution was to change the first 5 lines to to
scopes = ['https://www.googleapis.com/auth/calendar', 'https://www.googleapis.com/auth/calendar.events']
authorization = Google::Auth.get_application_default(scopes)
auth_client = authorization.dup
auth_client.sub = 'account@mydomain.com'
auth_client.fetch_access_token!
client = Google::Apis::CalendarV3::CalendarService.new
client.authorization = auth_client
来源:https://stackoverflow.com/questions/56483564/google-calendar-api-you-need-to-have-writer-access-to-this-calendar