我觉得自己很笨。我得到了授权代码,现在可以访问驱动器API,这正是我几乎想要的。
我希望能够离线访问API,但我仍然不明白如何保存凭据。
我的问题是,我应该在这个方法中放入什么?
# Store OAuth 2.0 credentials in the application's database.
#
# @param [String] user_id
# User's ID.
# @param [Signet::OAuth2::Client] credentials
# OAuth 2.0 credentials to store.
def store_credentials(user_id, credentials)
raise NotImplementedError, 'store_credentials is not implemented.'
end
所以我以后可以运行:
client = Google::APIClient.new
client.authorization = credentials
client = client.discovered_api('drive', 'v2')
client
无需再次向用户请求权限
您可以将其写入一个文件:
def store_credentials(user_id, credentials)
File.write 'credentials.data', [user_id, credentials].to_json
end
然后从文件中读取:
require 'json'
user_id, credentials = *JSON.parse(File.read('credentials.data'))
client.authorization = credentials