我尝试在我的应用中为Google API实施3版认证,以便能够访问注册用户'谷歌日历。
在quickstart Ruby guide中,出现这个命令,据我所知,应该指向用户的令牌:
token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
它希望令牌存储in a file(或Redis),但(当然)我将每个用户的令牌存储在我的数据库中(Postgres)。
我是否错误地了解了命令的用途或其他方式 - 我如何在数据库存储中使用它?
答案 0 :(得分:3)
我是根据@ Rafe的回答自己实现的。只是想分享以防有人想要复制ActiveRecord /数据库存储实现:
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
答案 1 :(得分:1)
根据the readme:
自行实施也可以使用自定义存储实现。有关其他详细信息,请参阅token_store.rb。
根据上述文件的外观,使用ActiveRecord(或其他ORM)实现load(id)
,store(id, token)
和delete(id)
并不太难。