Fel*_*ger 4 ruby ruby-on-rails google-api google-oauth
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.
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)
Run Code Online (Sandbox Code Playgroud)
它期望令牌存储在文件(或Redis)中,但是(当然)我将每个用户的令牌存储在我的数据库(Postgres)中。
我是否错误地或以其他方式理解了命令的目的 - 如何将它与数据库存储一起使用?
我根据@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
Run Code Online (Sandbox Code Playgroud)
根据自述文件,自己实现:
还可以使用自定义存储实现。有关更多详细信息,请参阅token_store.rb。
从上述文件的外观来看,使用 ActiveRecord(或另一个 ORM)实现load(id)
,store(id, token)
和应该不会太难。delete(id)
归档时间: |
|
查看次数: |
1092 次 |
最近记录: |