【问题标题】: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? 还是将凭据保存到数据库中以备下次使用?

【问题讨论】:

  • 任何人都可以添加“google-auth-library-ruby”标签吗?在官方的谷歌代表 - github.com/google/google-auth-library-ruby 我们在支持部分有此类标签的链接(提问链接)。但它实际上不在 SO 中

标签: 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)

【讨论】:

    猜你喜欢
    • 2021-04-24
    • 2020-12-11
    • 1970-01-01
    • 2014-11-07
    • 2015-11-26
    • 2012-02-15
    • 2014-07-08
    • 2013-02-21
    • 2015-04-09
    相关资源
    最近更新 更多