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:



367
368
369
370
371
372
373
374
375
376
377
378
379
380
# File 'lib/dropbox_sdk.rb', line 367

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.



402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/dropbox_sdk.rb', line 402

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



382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
# File 'lib/dropbox_sdk.rb', line 382

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