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”.



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

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



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

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.



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

def access_token
    @access_token
end

#assert_authorizedObject

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



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

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)


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

def authorized?
    !!@access_token
end

#clear_access_tokenObject

Clears the access_token



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

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



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

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.



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

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.



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

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:



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

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.



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

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



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

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



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

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



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

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