问题描述
我正在尝试在我的应用程序中对Google API进行三腿式身份验证,以便能够访问注册用户的Google日历.
I'm trying to implement a 3 legged authentication in my app to the Google API, to be able to access registered users' Google Calendars.
在快速入门Ruby指南中,该命令显示为据我了解,应该指向用户的令牌:
In the quickstart Ruby guide, this command comes up that as far as I understand should point to the user's tokens:
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
它期望将令牌存储在文件中(或 Redis ),但是(当然)我将每个用户的令牌存储在数据库(Postgres)中.
It expects the tokens stored in a file (or Redis), but (of course) I store each user's tokens in my database (Postgres).
我是否错误地理解了命令的用途-如何将其与数据库存储一起使用?
Have I understand the purpose of the command wrongly or otherwise - how do I use it with a database store?
推荐答案
我是根据@Rafe的答案自己实现的.只是想分享,以防有人想复制ActiveRecord/数据库存储实现:
I implemented it myself based on @Rafe's answer. Just wanted to share in case someone wants to copy the ActiveRecord / Database store implementation:
module Google
module Auth
module Stores
class DatabaseTokenStore < Google::Auth::TokenStore
def load(id)
user = User.find(id)
{
"client_id": ENV['google_client_id'],
"access_token": user.token,
"refresh_token": user.refresh_token,
"scope": ENV['google_scopes'],
"expiration_time_millis": user.token_expires_at
}.to_json
end
def store(id, token)
user = User.find(id)
hsh = JSON.parse(token)
user.update(
token: hsh["access_token"],
token_expires_at: hsh["expiration_time_millis"] / 1000
)
end
end
end
end
end
这篇关于将Google :: Auth :: Stores :: FileTokenStore与数据库一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!