首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Google Calendar API中使用google-api-ruby-client?

如何在Google Calendar API中使用google-api-ruby-client?
EN

Stack Overflow用户
提问于 2012-07-01 05:23:51
回答 3查看 2.2K关注 0票数 2

我一直在阅读Google Calendar API和google-api-ruby-client库的文档,但我在理解它们时遇到了很多问题。

我有一个Rails应用程序,它有一个前端,允许用户创建名为Events的对象,并将它们保存在我的服务器上的数据库中。

在弄清楚如何使用ruby库对API进行身份验证时,我遇到了很多问题。对我来说,使用OAuth2没有任何意义,因为我不需要向用户授权任何东西,因为我对他们的数据不感兴趣。我查看了服务帐户(http://code.google.com/p/google-api-ruby-client/wiki/ServiceAccounts),但看起来服务帐户不支持谷歌日历。

有谁有什么想法吗?这是我正在试验的代码(使用服务帐户):

代码语言:javascript
复制
@client = Google::APIClient.new(:key => 'my_api_key')
path_to_key_file = '/somepath/aaaaaa-privatekey.p12'
passphrase = 'my_pass_phrase'
key = Google::APIClient::PKCS12.load_key(path_to_key_file, passphrase)
asserter = Google::APIClient::JWTAsserter.new(
        'blah_blah@developer.gserviceaccount.com',
        'https://www.googleapis.com/auth/calendar',
        key)
# To request an access token, call authorize:
@client.authorization = asserter.authorize()

calendar = @client.discovered_api('calendar', 'v3')

event = {
  'summary' => 'Appointment',
  'location' => 'Somewhere',
  'start' => {
      'dateTime' => '2012-06-03T10:00:00.000-07:00'
   },
   'end' => {
      'dateTime' => '2012-06-03T10:25:00.000-07:00'
    },
    'attendees' => [
        {
          'email' => 'attendeeEmail'
        },
        #...
      ]
   }

result = @client.execute!(:api_method => calendar.events.insert,
                            :parameters => {'calendarId' => 'primary'},
                            :body => JSON.dump(event),
                            :headers => {'Content-Type' => 'application/json'})

当然,我会收到这个错误消息: Google::APIClient::ClientError (用户必须注册Google Calendar)。因为服务帐户不支持Google日历。

EN

回答 3

Stack Overflow用户

发布于 2012-10-24 09:41:43

我认为您仍然需要一个真正的google用户来托管日历实例。但是,一旦你以你的身份创建了日历,你就可以与服务帐户共享它。在日历的共享设置中,只需使用服务帐户的电子邮件地址(我的服务帐户以@developer.gserviceaccount.com结尾)。有了正确的共享权限,你的服务帐户可以创建/更改事件信息,而不会扰乱你的特定身份。从那里,您可以与更多的人(或公众)共享日历,以供他们消费镜像事件。

我遇到的另一个问题是,您似乎只能在每个到期期限内授权()服务帐户一次。您必须保存您获得的令牌,并在接下来的一个小时内重复使用它,然后获取一个新的令牌。

票数 1
EN

Stack Overflow用户

发布于 2013-01-06 05:00:28

我对露比一无所知。但看起来理解底层的REST查询将有助于调试您的问题。我在这里记录了它们:http://www.tqis.com/eloquency/googlecalendar.htm

票数 0
EN

Stack Overflow用户

发布于 2015-03-20 21:47:39

我在这方面也遇到了问题,最终解决了它。归根结底,Google Calendar API v3需要OAuth,您需要通过Google Developer Console设置一个应用程序/项目,然后在目标Google帐户上请求OAuth权限。一旦授权被授予,您将希望保存刷新令牌,并在后续调用中使用它来获取新的访问令牌(将过期!)。我在这里写了一篇详细的博客文章:http://www.geekytidbits.com/google-calendar-api-from-ruby/,这是我的示例脚本,希望能帮助您理解流程:

代码语言:javascript
复制
#gem install 'google-api-client'

require 'google/api_client'

#Setup auth client
client_secrets = Google::APIClient::ClientSecrets.load #client_secrets.json must be present in current directory!
auth_client = client_secrets.to_authorization
auth_client.update!(
  :scope => 'https://www.googleapis.com/auth/calendar',
  :access_type => "offline", #will make refresh_token available
  :approval_prompt =>'force',
  :redirect_uri => 'http://www.myauthorizedredirecturl.com'
)

refresh_token_available = File.exist?('refresh_token.txt')

if !refresh_token_available
 #OAuth URL - this is the url that will prompt a Google Account owner to give access to this app.
 puts "Navigate browser to: '#{auth_client.authorization_uri.to_s}' and copy/paste auth code after redirect."

 #Once the authorization_uri (above) is followed and authorization is given, a redirect will be made 
 #to http://www.myauthorizedredirecturl.com (defined above) and include the auth code in the request url.
 print "Auth code: "
 auth_client.code = gets
else 
 #If authorization has already been given and refresh token saved previously, simply set the refresh code here.
 auth_client.refresh_token = File.read('refresh_token.txt')
end

#Now, get our access token which is what we will need to work with the API.
auth_client.fetch_access_token!

if !refresh_token_available
    #Save refresh_token for next time
    #Note: auth_client.refresh_token is only available the first time after OAuth permission is granted.  
    #If you need it again, the Google Account owner would have deauthorize your app and you would have to request access again.
    #Therefore, it is important that the refresh token is saved after authenticating the first time!
    File.open('refresh_token.txt', 'w') { |file| file.write(auth_client.refresh_token) }
    refresh_token_available = true
end

api_client = Google::APIClient.new
cal = api_client.discovered_api('calendar', 'v3')

#Get Event List
puts "Getting list of events..."
list = api_client.execute(:api_method => cal.events.list, 
    :authorization => auth_client,
    :parameters => {
        'maxResults' => 20, 
        'timeMin' => '2014-06-18T03:12:24-00:00', 
        'q' => 'Meeting', 
        'calendarId' => 'primary'})

puts "Fetched #{list.data.items.count} events..."

#Update Event
puts "Updating first event from list..."
update_event = list.data.items[0]
update_event.description = "Updated Description here"
result = api_client.execute(:api_method => cal.events.update, 
    :authorization => auth_client,
    :parameters => { 'calendarId' => 'primary', 'eventId' => update_event.id}, 
    :headers => {'Content-Type' => 'application/json'},
    :body_object => update_event)
puts "Done with update."

#Add New Event
puts "Inserting new event..."
new_event = cal.events.insert.request_schema.new
new_event.start = { 'date' => '2015-01-01' } #All day event
new_event.end = { 'date' => '2015-01-01' } 
new_event.description = "Description here"
new_event.summary = "Summary here"
result = api_client.execute(:api_method => cal.events.insert, 
    :authorization => auth_client,
    :parameters => { 'calendarId' => 'primary'}, 
    :headers => {'Content-Type' => 'application/json'},
    :body_object => new_event)
puts "Done with insert."
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11277789

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档