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



235
236
237
238
239
240
241
# File 'lib/dropbox_sdk.rb', line 235

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



385
386
387
388
389
390
391
392
393
394
# File 'lib/dropbox_sdk.rb', line 385

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.



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

def access_token
  @access_token
end

#assert_authorizedObject

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



354
355
356
357
358
# File 'lib/dropbox_sdk.rb', line 354

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)


361
362
363
# File 'lib/dropbox_sdk.rb', line 361

def authorized?
  !!@access_token
end

#clear_access_tokenObject

Clears the access_token



312
313
314
# File 'lib/dropbox_sdk.rb', line 312

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



343
344
345
346
347
348
349
350
351
# File 'lib/dropbox_sdk.rb', line 343

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.



297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/dropbox_sdk.rb', line 297

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.



291
292
293
# File 'lib/dropbox_sdk.rb', line 291

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:



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/dropbox_sdk.rb', line 273

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.



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

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



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

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



336
337
338
# File 'lib/dropbox_sdk.rb', line 336

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



329
330
331
# File 'lib/dropbox_sdk.rb', line 329

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