【问题标题】:Get Refresh Token with lib google-drive-ruby使用 lib google-drive-ruby 获取刷新令牌
【发布时间】:2018-04-09 12:24:23
【问题描述】:
来自文章:https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md
代码:
credentials = ... same as above ...
credentials.code = authorization_code
credentials.fetch_access_token!
然后
如果您想在之后恢复会话,您可以在 credentials.fetch_access_token 之后存储 credentials.refresh_token!
但是在 credentials.fetch_access_token 之后!
credentials.refresh_token 为 nil
如何获得 refresh_token?
还是将凭据保存到数据库中以备下次使用?
【问题讨论】:
标签:
ruby
google-auth-library-ruby
【解决方案1】:
需要添加附加参数:
require "googleauth"
credentials = Google::Auth::UserRefreshCredentials.new(
client_id: "YOUR CLIENT ID",
client_secret: "YOUR CLIENT SECRET",
scope: [
"https://www.googleapis.com/auth/drive",
"https://spreadsheets.google.com/feeds/",
],
redirect_uri: "http://example.com/redirect",
:additional_parameters => {
"access_type"=>"offline",
"include_granted_scopes"=>"true",
"prompt" => "consent"
}
)
auth_url = credentials.authorization_uri
credentials.code = authorization_code
credentials.fetch_access_token!
然后得到:
refresh_token = credentials.refresh_token
然后refresh_token就可以存入数据库,供以后使用:
credentials.refresh_token = refresh_token
credentials.fetch_access_token!
session = GoogleDrive::Session.from_credentials(credentials)