未创建Rails + Google Calendar API事件 [英] Rails + Google Calendar API events not created
问题描述
我创建了rails web应用程序。当用户进行某些操作时,我的应用必须在我的Google日历中创建新事件。对于我的任务,我选择了服务器到服务器授权。
I created rails web app. When user make some actions, my app must create new event in my Google Calendar. For my task I choose server-to-server authorization.
我写了下一个ruby代码:
I wrote next ruby code:
client = Google::APIClient.new({:application_name => "Rails calendar",
:application_version => "1.0"})
keypath = Rails.root.join('config','google_secrets.p12').to_s
key = Google::APIClient::PKCS12.load_key(keypath, "notasecret")
# generate request body for authorization
client.authorization = Signet::OAuth2::Client.new(
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
:audience => 'https://accounts.google.com/o/oauth2/token',
:scope => 'https://www.googleapis.com/auth/calendar',
:issuer => '***',
:signing_key => key).tap{ |auth| auth.fetch_access_token! }
api_method = client.discovered_api('calendar','v3').events.insert
@meeting_data = {
guests: [{email: '***@gmail.com'}, {email: '***@gmail.com'}],
start_time: "2016-08-10T19:00:00+03:00",
end_time: "2016-08-10T20:00:00+03:00",
topic: "Test meeting",
messages_thread_id: 2
}
event = {
summary: @meeting_data[:topic],
start: {
dateTime: @meeting_data[:start_time],
time_zone: 'Asia/Jerusalem'
},
end: {
dateTime: @meeting_data[:end_time],
time_zone: 'Asia/Jerusalem'
},
attendees: @meeting_data[:guests],
visibility: 'private',
reminders: {
useDefault: true,
}
}
result = client.execute(:api_method => api_method,
:parameters => {calendarId: 'primary'},
:body => JSON.dump(event),
:headers => {'Content-Type' => 'application/json'})
puts result.data.as_json
但是,当我尝试使用该代码和服务器返回结果为JSON,如果我去创建事件的网址,谷歌显示我的消息,该事件不存在。
But when i try to use that code and server return me result as JSON, if I go on url of "created" event, google show me message that event does not exist.
{"kind"=>"calendar#event", "etag"=>"***", "id"=>"***", "status"=>"confirmed", "htmlLink"=>"https://www.google.com/calendar/event?eid=***", "created"=>"2016-08-04T11:54:46.000Z", "updated"=>"2016-08-04T11:54:46.327Z", "summary"=>"Test meeting", "creator"=>{"email"=>"***", "self"=>true}, "organizer"=>{"email"=>"***", "self"=>true}, "start"=>{"dateTime"=>"2016-08-10T16:00:00Z", "timeZone"=>"Asia/Jerusalem"}, "end"=>{"dateTime"=>"2016-08-10T17:00:00Z", "timeZone"=>"Asia/Jerusalem"}, "visibility"=>"private", "iCalUID"=>"***", "sequence"=>0, "attendees"=>[{"email"=>"***@gmail.com", "displayName"=>"***", "responseStatus"=>"needsAction"}, {"email"=>"***@gmail.com", "displayName"=>"***", "responseStatus"=>"needsAction"}], "reminders"=>{"useDefault"=>true}}
推荐答案
我面临同样的问题。需要给日历的client_email写入规则。
I faced with same problem. Needed give write rules to "client_email" for calendar.
您使用组织日历。这就是为什么只有组织管理员才能做到这一点。
You use organization calendar. Thats why only organization admin can do it.
这篇关于未创建Rails + Google Calendar API事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!