已解决
对 Google Workspace 日历共享设置的更改大约需要 2 小时才能生效。
修复是与服务帐户共享日历并授予其管理事件的权限。
尝试通过脚本创建事件时,我不断收到“您需要拥有此日历的作者访问权限。(Google::Apis::ClientError)”错误。
我已完成并确保以下事项:
我目前不知所措,希望得到任何建议。
代码如下:
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
CREDENTIALS_PATH = File.join(File.dirname(__FILE__), 'credentials.json')
SCOPE = Google::Apis::CalendarV3::AUTH_CALENDAR_EVENTS
APPLICATION_NAME = "GCAL Events"
def authorize
credentials = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: File.open(CREDENTIALS_PATH),
scope: SCOPE
)
credentials.fetch_access_token!
credentials
end
def create_event(summary, start_time, end_time)
service = Google::Apis::CalendarV3::CalendarService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
event = Google::Apis::CalendarV3::Event.new({
summary: summary,
start: {
date_time: start_time,
time_zone: 'Pacific/Auckland'
},
end: {
date_time: end_time,
time_zone: 'Pacific/Auckland'
}
})
result = service.insert_event('[email protected]', event)
puts "Event created: #{result.html_link}"
end
create_event('xyz', '2023-03-03T09:00:00+12:00', '2023-03-03T10:00:00+12:00')
在 Google Workspace 中的其他用户的日历上创建活动
您需要告诉服务帐户要委托域中的哪个用户。
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
json_key_io: StringIO.new(File.read AppConfig[:credentials_file]),
scope: AppConfig[:cal_scope]).dup
authorizer.sub = @person
authorizer.fetch_access_token!