0

来自文章: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 为零

我怎样才能获得 refresh_token?还是将凭据保存到数据库中以备下次使用?

4

1 回答 1

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)
于 2018-04-18T10:20:21.737 回答