Class: DropboxSession

Inherits:
DropboxSessionBase show all
Defined in:
lib/dropbox_sdk.rb

Overview

DropboxSession is responsible for holding OAuth 1 information. It knows how to take your consumer key and secret and request an access token, an authorize url, and get an access token. You just need to pass it to DropboxClient after its been authorized.

Instance Attribute Summary

Attributes inherited from DropboxSessionBase

#locale

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DropboxSessionBase

#do_get, #do_http_with_body, #do_post, #do_put

Constructor Details

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

  • consumer_key - Your Dropbox application’s “app key”.

  • consumer_secret - Your Dropbox application’s “app secret”.



222
223
224
225
226
227
228
# File 'lib/dropbox_sdk.rb', line 222

def initialize(consumer_key, consumer_secret, locale=nil)
    super(locale)
    @consumer_key = consumer_key
    @consumer_secret = consumer_secret
    @request_token = nil
    @access_token = nil
end

Class Method Details

.deserialize(ser) ⇒ Object

Takes a serialized DropboxSession YAML String and returns a new DropboxSession object



372
373
374
375
376
377
378
379
380
381
# File 'lib/dropbox_sdk.rb', line 372

def self.deserialize(ser)
    ser = YAML::load(ser)
    session = DropboxSession.new(ser.pop, ser.pop)
    session.set_request_token(ser.pop, ser.pop)

    if ser.length > 0
        session.set_access_token(ser.pop, ser.pop)
    end
    session
end

Instance Method Details

#access_tokenObject

Returns the access token, or nil if one hasn’t been acquired yet.



309
310
311
# File 'lib/dropbox_sdk.rb', line 309

def access_token
    @access_token
end

#assert_authorizedObject

If we have an access token, then do nothing. If not, throw a RuntimeError.



341
342
343
344
345
# File 'lib/dropbox_sdk.rb', line 341

def assert_authorized
    unless authorized?
        raise RuntimeError.new('Session does not yet have a request token')
    end
end

#authorized?Boolean

Returns true if this Session has been authorized and has an access_token.

Returns:

  • (Boolean)


348
349
350
# File 'lib/dropbox_sdk.rb', line 348

def authorized?
    !!@access_token
end

#clear_access_tokenObject

Clears the access_token



299
300
301
# File 'lib/dropbox_sdk.rb', line 299

def clear_access_token
    @access_token = nil
end

#get_access_tokenObject

Returns the access token. If this DropboxSession doesn’t yet have an access_token, it requests one using the request_token generate from your app’s token and secret. This request will fail unless your user has gone to the authorize_url and approved your request



330
331
332
333
334
335
336
337
338
# File 'lib/dropbox_sdk.rb', line 330

def get_access_token
    return @access_token if authorized?

    if @request_token.nil?
        raise RuntimeError.new("No request token. You must set this or get an authorize url first.")
    end

    @access_token = get_token("/access_token", @request_token,  "Couldn't get access token.")
end

#get_authorize_url(callback = nil) ⇒ Object

This returns a URL that your user must visit to grant permissions to this application.



284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/dropbox_sdk.rb', line 284

def get_authorize_url(callback=nil)
    get_request_token()

    url = "/#{Dropbox::API_VERSION}/oauth/authorize?oauth_token=#{URI.escape(@request_token.key)}"
    if callback
        url += "&oauth_callback=#{URI.escape(callback)}"
    end
    if @locale
        url += "&locale=#{URI.escape(@locale)}"
    end

    "https://#{Dropbox::WEB_SERVER}#{url}"
end

#get_request_tokenObject

This returns a request token. Requests one from the dropbox server using the provided application key and secret if nessecary.



278
279
280
# File 'lib/dropbox_sdk.rb', line 278

def get_request_token()
    @request_token ||= get_token("/request_token", nil, "Error getting request token.  Is your app key and secret correctly set?")
end

#get_token(url_end, input_token, error_message_prefix) ⇒ Object

: nodoc:



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/dropbox_sdk.rb', line 260

def get_token(url_end, input_token, error_message_prefix) #: nodoc:
    response = do_get_with_token("https://#{Dropbox::API_SERVER}:443/#{Dropbox::API_VERSION}/oauth#{url_end}", input_token)
    if not response.kind_of?(Net::HTTPSuccess) # it must be a 200
        raise DropboxAuthError.new("#{error_message_prefix}  Server returned #{response.code}: #{response.message}.", response)
    end
    parts = CGI.parse(response.body)

    if !parts.has_key? "oauth_token" and parts["oauth_token"].length != 1
        raise DropboxAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response)
    end
    if !parts.has_key? "oauth_token_secret" and parts["oauth_token_secret"].length != 1
        raise DropboxAuthError.new("Invalid response from #{url_end}: missing \"oauth_token\" parameter: #{response.body}", response)
    end

    OAuthToken.new(parts["oauth_token"][0], parts["oauth_token_secret"][0])
end

#request_tokenObject

Returns the request token, or nil if one hasn’t been acquired yet.



304
305
306
# File 'lib/dropbox_sdk.rb', line 304

def request_token
    @request_token
end

#serializeObject

serialize the DropboxSession. At DropboxSession’s state is capture in three key/secret pairs. Consumer, request, and access. Serialize returns these in a YAML string, generated from a converted array of the form:

consumer_key, consumer_secret, request_token.token, request_token.secret, access_token.token, access_token.secret

access_token is only included if it already exists in the DropboxSesssion



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/dropbox_sdk.rb', line 357

def serialize
    toreturn = []
    if @access_token
        toreturn.push @access_token.secret, @access_token.key
    end

    get_request_token

    toreturn.push @request_token.secret, @request_token.key
    toreturn.push @consumer_secret, @consumer_key

    toreturn.to_yaml
end

#set_access_token(key, secret) ⇒ Object

Given a saved access token and secret, you set this Session to use that token and secret

  • token - this is the access token

  • secret - this is the access token secret



323
324
325
# File 'lib/dropbox_sdk.rb', line 323

def set_access_token(key, secret)
    @access_token = OAuthToken.new(key, secret)
end

#set_request_token(key, secret) ⇒ Object

Given a saved request token and secret, set this location’s token and secret

  • token - this is the request token

  • secret - this is the request token secret



316
317
318
# File 'lib/dropbox_sdk.rb', line 316

def set_request_token(key, secret)
    @request_token = OAuthToken.new(key, secret)
end