【发布时间】:2013-06-14 22:53:07
【问题描述】:
我的 rails 应用程序将允许创建约会。然后应将这些约会导出到每个用户的谷歌日历。
我已经安装了 omniauth-google-oauth2,并获得了我的 api 密钥、client_id 和 client_secret来自 Google API 控制台的 Web 应用程序。
我可以使用 omniauth 对用户进行身份验证,并将 access_token、refresh_token 和 expires_at 日期存储到我的数据库中, 供以后使用。
当需要将事件导出到每个用户的 google 日历(在后台作业中完成)时,我收到了 Invalid Credentials 的响应。
我做错了什么?
注意:在oauth授权后我可以立即访问用户日历,但过了一段时间就不行了。
代码:
sessions_controller
/auth/:provider/callback 路由到这个方法:
auth = request.env["omniauth.auth"]
# Save token of user
current_user.google_auth_token = auth["credentials"]["token"]
current_user.google_auth_refresh_token = auth["credentials"]["refresh_token"]
current_user.google_auth_expires_at = Time.at auth["credentials"]["expires_at"]
current_user.save
omniauth 配置
Rails.application.config.middleware.use OmniAuth::Builder do
provider :google_oauth2, ENV["GOOGLE_CLIENT_ID"], ENV["GOOGLE_CLIENT_SECRET"], {
access_type: 'offline',
scope: 'userinfo.email,calendar'
}
end
获得创建用户日历的权限
client = Google::APIClient.new
client.authorization.refresh_token = app.carer.google_auth_refresh_token
client.authorization.access_token = app.carer.google_auth_token
if client.authorization.refresh_token && client.authorization.expired?
client.authorization.fetch_access_token!
end
service = client.discovered_api('calendar', 'v3')
result = client.execute(:api_method => service.calendar_list.list)
# results in auth error, Invalid Credentials ...
【问题讨论】:
标签: ruby-on-rails ruby omniauth google-calendar-api google-api-ruby-client