Search code examples
google-api-ruby-client

is there a pure command-line oauth flow for installed apps?


I'm trying to get a pure command line oauth flow for an installed app and it's not easy to piece this together... Docs are sorely lacking... I started with the drive example (https://github.com/google/google-api-ruby-client-samples/tree/master/drive) but when it gets to client.authorization = flow.authorize(file_storage) it tries to start webrick to put up a web page. I need something that works similarly to the CLI tools provided by google: it needs to print out the URL I need to visit and then read in the response that I can copy&paste. Is this possible with the google ruby client?


Solution

  • Looks like the following monkey-patch works:

    module Google
      class APIClient
        class InstalledAppFlow
          def authorize_cli(storage)
            puts "Please visit: #{@authorization.authorization_uri.to_s}"
            printf "Enter the code: code="
            code = gets
            @authorization.code = code
            @authorization.fetch_access_token!
            if @authorization.access_token
              if storage.respond_to?(:write_credentials)
                storage.write_credentials(@authorization)
              end
              @authorization
            else
              nil
            end
          end
        end
      end
    end