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:



327
328
329
330
331
332
333
334
335
336
337
338
339
340
# File 'lib/dropbox_sdk.rb', line 327

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.



368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/dropbox_sdk.rb', line 368

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,
    }

    if not @locale.nil?
        params['locale'] = @locale
    end
    if not original_redirect_uri.nil?
        params['redirect_uri'] = original_redirect_uri
    end

    form_data = {}
    params.each {|k,v| form_data[k.to_s] = v if !v.nil?}
    request.set_form_data(form_data)

    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



342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/dropbox_sdk.rb', line 342

def _get_authorize_url(redirect_uri, state)
    params = {"client_id" => @consumer_key, "response_type" => "code"}
    if not redirect_uri.nil?
        params["redirect_uri"] = redirect_uri
    end
    if not state.nil?
        params["state"] = state
    end
    if not @locale.nil?
        params['locale'] = @locale
    end

    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 = params.collect {|k,v|
        CGI.escape(k) + "=" + CGI.escape(v)
    }.join("&")

    target.to_s
end