Class: DropboxOAuth2FlowBase

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox_sdk.rb

Overview

Base class for the two OAuth 2 authorization helpers.

Instance Method Summary collapse

Constructor Details

#initialize(consumer_key, consumer_secret, locale = nil) ⇒ DropboxOAuth2FlowBase

:nodoc:



421
422
423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/dropbox_sdk.rb', line 421

def initialize(consumer_key, consumer_secret, locale=nil)
  if not consumer_key.is_a?(String)
    raise ArgumentError, "consumer_key must be a String, got #{consumer_key.inspect}"
  end
  if not consumer_secret.is_a?(String)
    raise ArgumentError, "consumer_secret must be a String, got #{consumer_secret.inspect}"
  end
  if not (locale.nil? or locale.is_a?(String))
    raise ArgumentError, "locale must be a String or nil, got #{locale.inspect}"
  end
  @consumer_key = consumer_key
  @consumer_secret = consumer_secret
  @locale = locale
end

Instance Method Details

#_finish(code, original_redirect_uri) ⇒ Object

Finish the OAuth 2 authorization process. If you used a redirect_uri, pass that in. Will return an access token string that you can use with DropboxClient.



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
# File 'lib/dropbox_sdk.rb', line 456

def _finish(code, original_redirect_uri)
  if not code.is_a?(String)
    raise ArgumentError, "code must be a String"
  end

  uri = URI.parse("https://#{Dropbox::API_SERVER}/1/oauth2/token")
  request = Net::HTTP::Post.new(uri.request_uri)
  client_credentials = @consumer_key + ':' + @consumer_secret
  request.add_field('Authorization', 'Basic ' + Base64.encode64(client_credentials).chomp("\n"))

  params = {
    "grant_type" => "authorization_code",
    "code" => code,
    "redirect_uri" => original_redirect_uri,
    "locale" => @locale,
  }

  request.set_form_data(Dropbox::clean_params(params))

  response = Dropbox::do_http(uri, request)

  j = Dropbox::parse_response(response)
  ["token_type", "access_token", "uid"].each { |k|
    if not j.has_key?(k)
      raise DropboxError.new("Bad response from /token: missing \"#{k}\".")
    end
    if not j[k].is_a?(String)
      raise DropboxError.new("Bad response from /token: field \"#{k}\" is not a string.")
    end
  }
  if j["token_type"] != "bearer" and j["token_type"] != "Bearer"
    raise DropboxError.new("Bad response from /token: \"token_type\" is \"#{token_type}\".")
  end

  return j['access_token'], j['uid']
end

#_get_authorize_url(redirect_uri, state) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/dropbox_sdk.rb', line 436

def _get_authorize_url(redirect_uri, state)
  params = {
    "client_id" => @consumer_key,
    "response_type" => "code",
    "redirect_uri" => redirect_uri,
    "state" => state,
    "locale" => @locale,
  }

  host = Dropbox::WEB_SERVER
  path = "/#{Dropbox::API_VERSION}/oauth2/authorize"

  target = URI::Generic.new("https", nil, host, nil, nil, path, nil, nil, nil)
  target.query = Dropbox::make_query_string(params)

  target.to_s
end