Get Refresh Token with lib google-drive-ruby

476 Views Asked by At

From article: https://github.com/gimite/google-drive-ruby/blob/master/doc/authorization.md

Code:

credentials = ... same as above ...
credentials.code = authorization_code
credentials.fetch_access_token!

Then

If you want to restore a session afterwards, you can store credentials.refresh_token after credentials.fetch_access_token!

But after credentials.fetch_access_token! credentials.refresh_token is nil

How i can get refresh_token? Or save credentials to database for next time?

1

There are 1 best solutions below

1
On

Need add additional_parameters:

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!

then get:

refresh_token = credentials.refresh_token 

and then refresh_token can be stored in database and used later:

credentials.refresh_token = refresh_token
credentials.fetch_access_token!
session = GoogleDrive::Session.from_credentials(credentials)