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



181
182
183
184
185
186
187
# File 'lib/dropbox_sdk.rb', line 181

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



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

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.



268
269
270
# File 'lib/dropbox_sdk.rb', line 268

def access_token
    @access_token
end

#assert_authorizedObject

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



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

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)


307
308
309
# File 'lib/dropbox_sdk.rb', line 307

def authorized?
    !!@access_token
end

#clear_access_tokenObject

Clears the access_token



258
259
260
# File 'lib/dropbox_sdk.rb', line 258

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



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

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.



243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/dropbox_sdk.rb', line 243

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.



237
238
239
# File 'lib/dropbox_sdk.rb', line 237

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:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/dropbox_sdk.rb', line 219

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.



263
264
265
# File 'lib/dropbox_sdk.rb', line 263

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



316
317
318
319
320
321
322
323
324
325
326
327
328
# File 'lib/dropbox_sdk.rb', line 316

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



282
283
284
# File 'lib/dropbox_sdk.rb', line 282

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



275
276
277
# File 'lib/dropbox_sdk.rb', line 275

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