Class: Skydrive::Oauth::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/skydrive/oauth/client.rb

Overview

Oauth client class

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id, client_secret, callback_url, scope, opts = {}) ⇒ Client

Returns a new instance of Client.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/skydrive/oauth/client.rb', line 8

def initialize(client_id, client_secret, callback_url, scope, opts={})
  @client_id = client_id
  @client_secret = client_secret
  @callback_url = callback_url
  @scope = scope
  ssl = opts.delete(:ssl)

  @options = {
              :site => 'https://login.live.com/',
              :authorize_url => "oauth20_authorize.srf?response_type=code&scope=#{scope}",
              :token_url => 'oauth20_token.srf',
              :parse_json => true}.merge(opts)
  @options[:connection_opts][:ssl] = ssl if ssl
  @oauth_client = OAuth2::Client.new(client_id, client_secret, @options)
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def access_token
  @access_token
end

#callback_urlObject

Returns the value of attribute callback_url.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def callback_url
  @callback_url
end

#client_idObject

Returns the value of attribute client_id.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def client_id
  @client_id
end

#client_secretObject

Returns the value of attribute client_secret.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def client_secret
  @client_secret
end

#oauth_clientObject

Returns the value of attribute oauth_client.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def oauth_client
  @oauth_client
end

#scopeObject

Returns the value of attribute scope.



7
8
9
# File 'lib/skydrive/oauth/client.rb', line 7

def scope
  @scope
end

Instance Method Details

#authorize_urlString

Step 1: URL for OAuth2 authorization of Microsoft Live

Returns:

  • (String)


26
27
28
# File 'lib/skydrive/oauth/client.rb', line 26

def authorize_url
  oauth_client.auth_code.authorize_url(:redirect_uri => callback_url)
end

#get_access_token(code) ⇒ OAuth2::AccessToken

Step 2: Get access token after authorizing user

Parameters:

  • code (String)

    The value extracted from the callback url param ‘code’

Returns:

  • (OAuth2::AccessToken)

    the access token



33
34
35
# File 'lib/skydrive/oauth/client.rb', line 33

def get_access_token code
  @access_token = oauth_client.auth_code.get_token(code, :redirect_uri => callback_url)
end

#get_access_token_from_hash(token, opts = {}) ⇒ OAuth2::AccessToken

Alternate Step2: Make an access token from already available data one of :header, :body or :query

Parameters:

  • token (String)

    the Access Token value

  • opts (Hash) (defaults to: {})

    the options to create the Access Token with

Options Hash (opts):

  • :refresh_token (String) — default: nil

    the refresh_token value

  • :expires_in (FixNum, String) — default: nil

    the number of seconds in which the AccessToken will expire

  • :expires_at (FixNum, String) — default: nil

    the epoch time in seconds in which AccessToken will expire

  • :mode (Symbol) — default: :header

    the transmission mode of the Access Token parameter value

  • :header_format (String) — default: 'Bearer %s'

    the string format to use for the Authorization header

  • :param_name (String) — default: 'access_token'

    the parameter name to use for transmission of the Access Token value in :body or :query transmission mode

Returns:

  • (OAuth2::AccessToken)

    the access token



48
49
50
# File 'lib/skydrive/oauth/client.rb', line 48

def get_access_token_from_hash token, opts={}
  @access_token = OAuth2::AccessToken.new(@oauth_client, token, opts)
end