Class: DropboxSession

Inherits:
DropboxSessionBase show all
Defined in:
lib/dropbox_sdk_v2.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”.



237
238
239
240
241
242
243
# File 'lib/dropbox_sdk_v2.rb', line 237

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



387
388
389
390
391
392
393
394
395
396
# File 'lib/dropbox_sdk_v2.rb', line 387

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.



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

def access_token
  @access_token
end

#assert_authorizedObject

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



356
357
358
359
360
# File 'lib/dropbox_sdk_v2.rb', line 356

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)


363
364
365
# File 'lib/dropbox_sdk_v2.rb', line 363

def authorized?
  !!@access_token
end

#clear_access_tokenObject

Clears the access_token



314
315
316
# File 'lib/dropbox_sdk_v2.rb', line 314

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



345
346
347
348
349
350
351
352
353
# File 'lib/dropbox_sdk_v2.rb', line 345

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.



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/dropbox_sdk_v2.rb', line 299

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.



293
294
295
# File 'lib/dropbox_sdk_v2.rb', line 293

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:



275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/dropbox_sdk_v2.rb', line 275

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.



319
320
321
# File 'lib/dropbox_sdk_v2.rb', line 319

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



372
373
374
375
376
377
378
379
380
381
382
383
384
# File 'lib/dropbox_sdk_v2.rb', line 372

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



338
339
340
# File 'lib/dropbox_sdk_v2.rb', line 338

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



331
332
333
# File 'lib/dropbox_sdk_v2.rb', line 331

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