Parent

DropboxSession

DropboxSession is responsible for holding OAuth 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.

Public Class Methods

deserialize(ser) click to toggle source

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

# File lib/dropbox_sdk.rb, line 234
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
new(consumer_key, consumer_secret) click to toggle source
  • consumer_key - Your Dropbox application's "app key".

  • consumer_secret - Your Dropbox application's "app secret".

# File lib/dropbox_sdk.rb, line 26
def initialize(consumer_key, consumer_secret)
    @consumer_key = consumer_key
    @consumer_secret = consumer_secret
    @request_token = nil
    @access_token = nil
end

Public Instance Methods

access_token() click to toggle source

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

# File lib/dropbox_sdk.rb, line 171
def access_token
    @access_token
end
assert_authorized() click to toggle source

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

# File lib/dropbox_sdk.rb, line 203
def assert_authorized
    unless authorized?
        raise RuntimeError.new('Session does not yet have a request token')
    end
end
authorized?() click to toggle source

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

# File lib/dropbox_sdk.rb, line 210
def authorized?
    !!@access_token
end
clear_access_token() click to toggle source

Clears the access_token

# File lib/dropbox_sdk.rb, line 161
def clear_access_token
    @access_token = nil
end
do_http_with_body(uri, request, body) click to toggle source
# File lib/dropbox_sdk.rb, line 85
def do_http_with_body(uri, request, body)
    if body != nil
        if body.is_a?(Hash)
            form_data = {}
            body.each {|k,v| form_data[k.to_s] = v if !v.nil?}
            request.set_form_data(form_data)
        elsif body.respond_to?(:read)
            if body.respond_to?(:length)
                request["Content-Length"] = body.length.to_s
            elsif body.respond_to?(:stat) && body.stat.respond_to?(:size)
                request["Content-Length"] = body.stat.size.to_s
            else
                raise ArgumentError, "Don't know how to handle 'body' (responds to 'read' but not to 'length' or 'stat.size')."
            end
            request.body_stream = body
        else
            s = body.to_s
            request["Content-Length"] = s.length
            request.body = s
        end
    end
    do_http(uri, @access_token, request)
end
get_access_token() click to toggle source

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

# File lib/dropbox_sdk.rb, line 192
def get_access_token
    return @access_token if authorized?

    if @request_token.nil?
        raise DropboxAuthError.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) click to toggle source

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

# File lib/dropbox_sdk.rb, line 146
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_token() click to toggle source

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

# File lib/dropbox_sdk.rb, line 140
def get_request_token()
    @request_token ||= get_token("/request_token", nil, "Error getting request token.  Is your app key and secret correctly set?")
end
request_token() click to toggle source

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

# File lib/dropbox_sdk.rb, line 166
def request_token
    @request_token
end
serialize() click to toggle source

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

# File lib/dropbox_sdk.rb, line 219
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) click to toggle source

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

# File lib/dropbox_sdk.rb, line 185
def set_access_token(key, secret)
    @access_token = OAuthToken.new(key, secret)
end
set_request_token(key, secret) click to toggle source

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

# File lib/dropbox_sdk.rb, line 178
def set_request_token(key, secret)
    @request_token = OAuthToken.new(key, secret)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.