Class: LinkedIn::OAuth2

Inherits:
OAuth2::Client
  • Object
show all
Defined in:
lib/linked_in/oauth2.rb

Overview

The LinkedIn::OAuth2::Client class. Inherits directly from [intreda/oauth2](github.com/intridea/oauth2)‘s `OAuth2::Client`

LinkedIn::OAuth2 sets the following default options:

  • site = “www.linkedin.com

  • token_url = “/uas/oauth2/accessToken”

  • authorize_url = “/uas/oauth2/authorization”

More details on LinkedIn’s Authorization process can be found here: developer.linkedin.com/documents/authentication

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id = LinkedIn.config.client_id, client_secret = LinkedIn.config.client_secret, options = {}) {|builder| ... } ⇒ OAuth2

Instantiate a new OAuth 2.0 client using your client ID (aka API Key) and client secret (aka Secret Key).

You should set the client_id and client_secret in the config.

LinkedIn.configure do |config|
  config.client_id     = ENV["LINKEDIN_CLIENT_ID"]
  config.client_secret = ENV["LINKEDIN_CLIENT_SECRET"]
end

This will let you initialize with zero arguments.

If you have already set the ‘client_id` and `client_secret` in your config, the first and only argument can be the `options` hash.

Parameters:

  • client_id (String) (defaults to: LinkedIn.config.client_id)

    the client_id value

  • client_secret (String) (defaults to: LinkedIn.config.client_secret)

    the client_secret value

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

    the options to create the client with

Options Hash (options):

  • :token_method (Symbol) — default: :post

    HTTP method to use to request token (:get or :post)

  • :connection_opts (Hash) — default: {}

    Hash of connection options to pass to initialize Faraday with

  • :max_redirects (FixNum) — default: 5

    maximum number of redirects to follow

  • :raise_errors (Boolean) — default: true

    whether or not to raise an error on malformed responses

Yields:

  • (builder)

    The Faraday connection builder



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/linked_in/oauth2.rb', line 42

def initialize(client_id=LinkedIn.config.client_id,
               client_secret=LinkedIn.config.client_secret,
               options = {}, &block)

  if client_id.is_a? Hash
    options = client_id
    client_id = LinkedIn.config.client_id
  end

  options = default_oauth_options(options)

  super client_id, client_secret, options, &block

  @redirect_uri = options[:redirect_uri]

  if self.options[:raise_errors]
    check_credentials!(client_id, client_secret)
  end
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



13
14
15
# File 'lib/linked_in/oauth2.rb', line 13

def access_token
  @access_token
end

Instance Method Details

#auth_code_url(options = {}) ⇒ Object

Generates the URL users use to sign into your application.

Once a user enters their LinkedIn credentials, they will be redirected to your ‘redirect_uri` with the `code` parameter attached to it. The value of the `code` parameter can be used to get an access token.

We recommend you set your ‘client_id, `client_secret`, and `redirect_uri` in the `LinkedIn.configure` block. They can also be passed in as options.

Parameters:

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

    the options to generate the url with

Options Hash (options):

  • :redirect_uri (String)

    The url that gets redirected to after a successful authentication. This must exactly match the redirect urls setup on your LinkedIn Application Settings page. This option is not required if you already set the redirect_uri in the config.

  • :scope (String)

    A string of requested permissions you want from users when they authenticate with your app. If these are set on yoru LinkedIn Application settings page, you do not need to pass them in. The string must be a space-sparated, case-sensitive list of available scopes. See available scopes on LinkedIn’s API documentation page.

  • :state (String)

    A long string used for CSRF protection. It is added as the ‘state` GET param in the redirect_uri

  • :raise_errors (Boolean) — default: true

    whether or not to raise an error on malformed responses



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/linked_in/oauth2.rb', line 90

def auth_code_url(options={})
  options = default_auth_code_url_options(options)

  if self.options[:raise_errors]
    check_redirect_uri!(options)
  end

  @redirect_uri = options[:redirect_uri]

  self.auth_code.authorize_url(options)
end

#get_access_token(code = nil, options = {}) ⇒ Object

Returns the access token string for the newly authenticated user.

It also sets the ‘access_token` field on this LinkedIn::OAuth2 instance.

The required ‘code`

Parameters:

  • code (String) (defaults to: nil)

    the auth code which is passed in as a GET parameter to your ‘redirect_uri` after users authenticate your app

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

Options Hash (options):

  • :redirect_uri (String)

    You normally should not have to pass in the redirect_uri again. If ‘auth_code_url` was called on this LinkedIn::OAuth2 instance, then the `redirect_uri` will already be set. This is because the `redirect_uri` in the access token request must exactly match the `redirect_uri` in the auth code url.

  • :raise_errors (Boolean) — default: true

    whether or not to raise an error on malformed responses



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/linked_in/oauth2.rb', line 120

def get_access_token(code=nil, options={})
  check_for_code!(code)
  options = default_access_code_options(options)

  if self.options[:raise_errors]
    check_access_code_url!(options)
  end

  tok = self.auth_code.get_token(code, options)
  self.access_token = LinkedIn::AccessToken.new(tok.token,
                                                tok.expires_in,
                                                tok.expires_at)
  return self.access_token
rescue ::OAuth2::Error => e
  raise OAuthError.new(e.response)
end