4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

在Rails下通过Google API Client访问Google Calendar接口

Last updated at Posted at 2013-06-09

本文主要介绍通过使用Google API Client来在Rails应用里来访问Google Calendar接口来访问日程信息。

本文的主要代码可以从这里下载

1. 在Google网站创建应用程序

Google API Console创建应用:

a. 打开网址https://code.google.com/apis/console
b. 点击左边API Access 菜单
c. 在API Access里面(右边)创建一个Cliend ID,注意Application Type选择Web Application

最主要的是确保callback地址正确,本例是 http://localhost:3000/gcalendar/callback,基本上这样就可以。如果调试的时候出现错误,可根据错误信息酌情修改API设置。

2. 在本地实现API调用

在服务端设置好之后,就可以在自己机器上实现API的调用代码了。

2.1. 安装 Google API Client

在工程根目录下编辑Gemfile,加入:

gem 'google-api-client'

然后执行:

bundle install

2.2. 实现Controller

首先,按oauth的流程,需要先生产一个认证地址,即用户将会被导向这个地址代表的Google服务器地址。这个地址通过API Client的

2.2.1. 生产并导向授权地址


@client = Google::APIClient.new
@client.authorization.client_id = '你的client id,从Google网站获得'
@client.authorization.client_secret = '你的secret,从Google网站获得'
# api 的类型
@client.authorization.scope = 'https://www.googleapis.com/auth/calendar'
# 根据自己情况设置callback
@client.authorization.redirect_uri = 'http://localhost:3000/gcalendar/callback'

# 用户将被导向此网址进行授权,
# 可以在view里提供此链接地址,或者直接redirect到这个网址。
@redirect_uri = @client.authorization.authorization_uri

上面的代码很简单,生产APIClient后,就往这个类的authorization里面设置相关属性,上面例子是最基本的用法。

2.2.2. 处理callback(授权后的回调)

在用户同意授权后,Google会调用此网址,我们需要在这个回调方法中处理里面的access token相关信息。这里只是简单的存到session里,供其它action使用:

  # Google API 服务器授权后的回调接口
  def callback
    # 设置code,从Google 服务器返回的
    @client.authorization.code = params[:code] if params[:code]
    # 取得access token等信息
    @client.authorization.fetch_access_token!
 
    # 将token相关信息保存到session
    session[:access_token] = @client.authorization.access_token
    session[:refresh_token] = @client.authorization.refresh_token
    session[:expires_in] = @client.authorization.expires_in
    session[:issued_at] = @client.authorization.issued_at
 
    redirect_to :action => 'events'
  end
 

2.2.3. 调用Calendar相关接口

在上面的callback里,如果授权成功页面会转向到events这个action下去,在这里我们就可以尝试调用Calendar相关的API了。这里我们使用了两个API的例子,一个是取得Calendar列表,另一个就是取得某一天的某个Calendar下面的event列表。代码如下:


  def events
 
    # 取得 calendar API的service
    @calendar = @client.discovered_api('calendar', 'v3')
    
    # 调用取得calendar 一览接口
    @calendarlist = @client.execute(:api_method => @calendar.calendar_list.list,
                              :authorization => @client.authorization)
 
    # 调用取得某一calendar里面某一天的events
    # 注意,日期格式要iso8601,且要注意时区信息
    day = Time.now
    @time_min = Time.local(day.year, day.month, day.day, 0,0,0).iso8601
    @time_max = Time.local(day.year, day.month, day.day, 23,59,59).iso8601
 
    @events = @client.execute(:api_method => @calendar.events.list,
                              :parameters => {'calendarId' => 'primary',
                                "timeMin" => @time_min,
                                "timeMax" => @time_max
                                },
                              :authorization => @client.authorization)
 
  end
 

请注意看一下里面的注释,尤其是跟日期相关的时区和格式问题。

3. 总结

看来,通过Google API Client来实现Google 相关服务的调用还是非常简单的,希望更多的第三方站点能集成Google的相关服务。
本文(部分)代码可以在github下载

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?