Class: Google::APIClient::InstalledAppFlow

Inherits:
Object
  • Object
show all
Defined in:
lib/google/api_client/auth/installed_app.rb

Overview

Small helper for the sample apps for performing OAuth 2.0 flows from the command line or in any other installed app environment.

Examples:


client = Google::APIClient.new
flow = Google::APIClient::InstalledAppFlow.new(
  :client_id => '691380668085.apps.googleusercontent.com',
  :client_secret => '...',
  :scope => 'https://www.googleapis.com/auth/drive'
)
client.authorization = flow.authorize

Constant Summary collapse

RESPONSE_BODY =
<<-HTML
  <html>
    <head>
      <script>
        function closeWindow() {
          window.open('', '_self', '');
          window.close();
        }
        setTimeout(closeWindow, 10);
      </script>
    </head>
    <body>You may close this window.</body>
  </html>
HTML

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ InstalledAppFlow

Configure the flow

Parameters:

  • options (Hash)

    The configuration parameters for the client.

Options Hash (options):

  • :port (Fixnum)

    Port to run the embedded server on. Defaults to 9292

  • :client_id (String)

    A unique identifier issued to the client to identify itself to the authorization server.

  • :client_secret (String)

    A shared symmetric secret issued by the authorization server, which is used to authenticate the client.

  • :scope (String)

    The scope of the access request, expressed either as an Array or as a space-delimited String.

See Also:

  • Signet::OAuth2::Client


68
69
70
71
72
73
74
75
# File 'lib/google/api_client/auth/installed_app.rb', line 68

def initialize(options)
  @port = options[:port] || 9292
  @authorization = Signet::OAuth2::Client.new({
    :authorization_uri => 'https://accounts.google.com/o/oauth2/auth',
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :redirect_uri => "http://localhost:#{@port}/"}.update(options)
  )
end

Instance Method Details

#authorize(storage = nil) ⇒ Signet::OAuth2::Client

Request authorization. Opens a browser and waits for response.

Parameters:

  • storage (Google::APIClient::Storage) (defaults to: nil)

    Optional object that responds to :write_credentials, used to serialize the OAuth 2 credentials after completing the flow.

Returns:

  • (Signet::OAuth2::Client)

    Authorization instance, nil if user cancelled.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/google/api_client/auth/installed_app.rb', line 86

def authorize(storage=nil)
  auth = @authorization

  server = WEBrick::HTTPServer.new(
    :Port => @port,
    :BindAddress =>"localhost",
    :Logger => WEBrick::Log.new(STDOUT, 0),
    :AccessLog => []
  )
  begin
    trap("INT") { server.shutdown }

    server.mount_proc '/' do |req, res|
      auth.code = req.query['code']
      if auth.code
        auth.fetch_access_token!
      end
      res.status = WEBrick::HTTPStatus::RC_ACCEPTED
      res.body = RESPONSE_BODY
      server.stop
    end

    Launchy.open(auth.authorization_uri.to_s)
    server.start
  ensure
    server.shutdown
  end
  if @authorization.access_token
    if storage.respond_to?(:write_credentials)
      storage.write_credentials(@authorization)
    end
    return @authorization
  else
    return nil
  end
end