在 rails 中设置 omniauth 后访问 Google 日历 API
Access Google Calendar API after setting up omniauth in rails
我有一个 rails 应用程序正在使用 gem 'omniauth-google-oauth2'。到目前为止一切正常。现在,我正在尝试在用户 link 他们的 Google 帐户之后提取用户日历。
看了几个教程,但是好像拉不出来日历信息。使用的所有教程:
gem 'google-api-client', '~> 0.7.0', 要求: 'google/api_client'
问题是,我试了很多次这么久,我很困惑可能是什么问题。
设置 Omniauth 后,如何在我的 rails 应用程序中提取 Google 日历信息?
假设用户已授权您的应用程序并且您已存储访问令牌和刷新令牌,并且访问令牌具有访问用户日历的范围。
首先初始化客户端,得到一个新的access_token并指定你要使用的服务及其版本
客户端=Google::APIClient.new(:auto_refresh_token => true)
client.authorization.access_token = 代币
client.authorization.refresh_token = refresh_token
client.authorization.client_id = client_id
client.authorization.client_secret = client_secret
代币 = client.authorization.fetch_access_token["access_token"]
服务 = client.discovered_api('calendar' , 'v3')
使用客户端对象调用任何api方法,这里我调用'events.list'从日历中获取用户事件
response = client.execute(api_method: service.events.list,
parameters: { 'calendarId' => 'primary' },
headers: { 'Content-Type' => 'application/json' })
我终于让它工作了。
我将 :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']
sby 能告诉我,Sebhastien 的解决方案和我的解决方案有什么区别吗? (我的代码还没有准备好,我还不知道我的解决方案是否可以得到新的 access_token。)
1.I 只是不明白我何时以及如何获得新的访问令牌以及我是否需要处理 refresh_token 和 expires_at 属性。
2.How (: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']
我一直在努力寻找正确的解决方案,使用 google-api-client version 0.9
and I was very grateful to run into 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 也可能有助于了解随着这个新 gem 版本的出现情况发生了怎样的变化。
我有一个 rails 应用程序正在使用 gem 'omniauth-google-oauth2'。到目前为止一切正常。现在,我正在尝试在用户 link 他们的 Google 帐户之后提取用户日历。
看了几个教程,但是好像拉不出来日历信息。使用的所有教程:
gem 'google-api-client', '~> 0.7.0', 要求: 'google/api_client'
问题是,我试了很多次这么久,我很困惑可能是什么问题。
设置 Omniauth 后,如何在我的 rails 应用程序中提取 Google 日历信息?
假设用户已授权您的应用程序并且您已存储访问令牌和刷新令牌,并且访问令牌具有访问用户日历的范围。
首先初始化客户端,得到一个新的access_token并指定你要使用的服务及其版本
客户端=Google::APIClient.new(:auto_refresh_token => true)
client.authorization.access_token = 代币
client.authorization.refresh_token = refresh_token
client.authorization.client_id = client_id
client.authorization.client_secret = client_secret
代币 = client.authorization.fetch_access_token["access_token"]
服务 = client.discovered_api('calendar' , 'v3')
使用客户端对象调用任何api方法,这里我调用'events.list'从日历中获取用户事件
response = client.execute(api_method: service.events.list, parameters: { 'calendarId' => 'primary' }, headers: { 'Content-Type' => 'application/json' })
我终于让它工作了。
我将 :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']
sby 能告诉我,Sebhastien 的解决方案和我的解决方案有什么区别吗? (我的代码还没有准备好,我还不知道我的解决方案是否可以得到新的 access_token。)
1.I 只是不明白我何时以及如何获得新的访问令牌以及我是否需要处理 refresh_token 和 expires_at 属性。
2.How (: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']
我一直在努力寻找正确的解决方案,使用 google-api-client version 0.9
and I was very grateful to run into 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 也可能有助于了解随着这个新 gem 版本的出现情况发生了怎样的变化。