在rails

时间:2015-10-06 05:28:40

标签: ruby-on-rails calendar google-calendar-api

我有一个使用gem'omniauth-google-oauth2'的rails应用。到目前为止一切正常。现在,我想在他们关联Google帐户后提取用户日历。

我已经按照了几个教程,但似乎我无法提取日历信息。使用的所有教程:

gem'google-api-client','〜> 0.7.0',要求:'google / api_client'

问题是,我已经尝试了这么久,我很困惑这个问题是什么。

在设置Omniauth后开始,如何在我的rails应用程序中提取Google日历信息?

4 个答案:

答案 0 :(得分:1)

我终于开始工作了。

我添加了:google_access_token,:google_refresh_token,以及:google_expires_at到我的用户模型。和使用:

@user.google_access_token  = request.env["omniauth.auth"]["credentials"]["token"]
@user.google_refresh_token = request.env["omniauth.auth"]["credentials"]["refresh_token"]
@user.google_expires_at    = Time.at request.env["omniauth.auth"]["credentials"]["expires_at"]
@user.save

在我的omniauth回调控制器中存储它以及访问它。

client = Google::APIClient.new(:auto_refresh_token => true)
client.authorization.access_token = current_user.google_access_token
client.authorization.refresh_token = current_user.google_refresh_token
client.authorization.client_id = ENV.fetch('google_id')
client.authorization.client_secret = ENV.fetch('google_secret')

if client.authorization.refresh_token && client.authorization.expired?
  client.authorization.fetch_access_token!
end

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

response = client.execute(api_method: service.events.list,
        parameters: { 'calendarId' => 'primary' },
          headers: { 'Content-Type' => 'application/json' })

@items = response.data['items']

答案 1 :(得分:1)

我一直在努力寻找使用google-api-client版本0.9的正确解决方案,我非常感谢您发表Sophie's blog post with Josh'评论。以下是我如何使用它们输入的快速回顾:

require 'google/apis/calendar_v3'
require 'google/api_client/client_secrets.rb'

secrets = Google::APIClient::ClientSecrets.new(
  { "web" => 
    { "access_token" => "YOUR_ACCESS_TOKEN", 
      "refresh_token" => "YOUR_REFRESH_TOKEN", 
      "client_id" => "YOUR_CLIENT_ID", 
      "client_secret" => "YOUR_CLIENT_SECRET"
    }
  }
)
cal = Google::Apis::CalendarV3::CalendarService.new
cal.authorization = secrets.to_authorization
cal.authorization.refresh!

data = cal.list_calendar_lists

通过这个新的宝石版本的出现,阅读migration to 0.9 manual以了解情况如何变化也可能有所帮助。

答案 2 :(得分:0)

假设用户已授权您的应用程序,并且您已存储访问令牌和刷新令牌,并且访问令牌具有访问用户日历的范围。

  • 首先初始化客户端并获取新的access_token并指定您要使用的服务及其版本

    client = Google :: APIClient.new(:auto_refresh_token => true)

    client.authorization.access_token = token

    client.authorization.refresh_token = refresh_token

    client.authorization.client_id = client_id

    client.authorization.client_secret = client_secret

    token = client.authorization.fetch_access_token [“access_token”]

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

  • 使用客户端对象调用任何api方法,这里我调用'events.list'来从日历中获取用户事件

    response = client.execute(api_method: service.events.list, parameters: { 'calendarId' => 'primary' }, headers: { 'Content-Type' => 'application/json' })

答案 3 :(得分:0)

请告诉我,Sebhastien和我的解决方案有什么区别? (我的代码还没有准备好,如果我能用我的解决方案获得新的access_token,我还不知道。)

1.我只是不知道何时以及如何获得新的访问令牌以及我是否需要处理refresh_token和expires_at属性。

2. (:auto_refresh_token => true)fetch_access_token!如何正常工作?

client = Google::APIClient.new
#next 4 lines get the data from app/db
client.authorization.access_token = google_account.token
client.authorization.client_id = ENV['GOOGLE_API_KEY']
client.authorization.client_secret = ENV['GOOGLE_API_SECRET']
client.authorization.refresh_token = google_account.refresh_token

old_token = client.authorization.access_token
service = client.discovered_api('calendar', 'v3')
result = client.execute(
  :api_method => service.calendar_list.list,
  :parameters => {},
  :headers => {'Content-Type' => 'application/json'})

new_token = client.authorization.access_token
if old_token != new_token
  Social.update_attribute(token: new_token) #saving new token to db
end

@items = result.data['items']