Class: Bitly::OAuth

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

Overview

The Oauth class handles interacting with the Bitly API to setup OAuth flows to redirect the user to authorize with Bitly and turn the resultant code into an OAuth access token.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client_id:, client_secret:) ⇒ Bitly::OAuth

Creates a new Bitly::OAuth client to retrieve user access tokens.

To initialize a Bitly::OAuth client you need a client_id and client_secret which you will get when you register your application at bitly.com/a/oauth_apps

Examples:

oauth = Bitly::OAuth.new(client_id: "test", client_secret: "secret")

Parameters:

Since:

  • 2.0.0



29
30
31
32
33
34
35
36
37
38
# File 'lib/bitly/oauth.rb', line 29

def initialize(client_id:, client_secret:)
  @client_id = client_id
  @client_secret = client_secret
  @client = OAuth2::Client.new(
    client_id,
    client_secret,
    :site => 'https://api-ssl.bitly.com',
    :token_url => '/oauth/access_token',
  )
end

Instance Attribute Details

#client_idObject (readonly)

Returns the value of attribute client_id.



11
12
13
# File 'lib/bitly/oauth.rb', line 11

def client_id
  @client_id
end

#client_secretObject (readonly)

Returns the value of attribute client_secret.



11
12
13
# File 'lib/bitly/oauth.rb', line 11

def client_secret
  @client_secret
end

Instance Method Details

#access_token(redirect_uri: nil, code: nil, username: nil, password: nil) ⇒ Object

When the user is redirected to your redirect URI, Bitly will include a code parameter. You then use the original redirect URI and the code to retrieve an access token.

Examples:

oauth.access_token(redirect_uri: "https://example.com/oauth/redirect", "code")
# => "9646c4f76e32c18f0afad3b3ed9524f3c917775b"


74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bitly/oauth.rb', line 74

def access_token(redirect_uri: nil, code: nil, username: nil, password: nil)
  begin
    if redirect_uri && code
      access_token_from_code(redirect_uri: redirect_uri, code: code)
    elsif username && password
      access_token_from_credentials(username: username, password: password)
    else
      raise ArgumentError, "not enough arguments, please include a redirect_uri and code or a username and password."
    end
  rescue OAuth2::Error => e
    raise Bitly::Error, Bitly::HTTP::Response.new(status: e.response.status.to_s, body: e.response.body, headers: e.response.headers)
  end
end

#authorize_uri(redirect_uri, state: nil) ⇒ String

Returns the URL that you need to redirect the user to, so they can give your app permission to use their Bitly account.

Examples:

oauth.authorize_uri("https://example.com/oauth/redirect")
# => "https://bitly.com/oauth/authorize?client_id=...&redirect_uri=https://example.com/oauth_page"

Parameters:

  • redirect_uri (String)

    The URI you want Bitly to redirect the user back to once they are authorized. This should match the redirect_uri you set in your Bitly OAuth app.

  • state (String) (defaults to: nil)

    An optional state that you can pass to the authorize URI that will be returned in the redirect.

Returns:

  • (String)

    The authorize URI that you should use to redirect your user.

Since:

  • 2.0.0



57
58
59
60
61
62
63
64
# File 'lib/bitly/oauth.rb', line 57

def authorize_uri(redirect_uri, state: nil)
  params = {
    redirect_uri: redirect_uri,
    client_id: client_id
  }
  params[:state] = state if state
  @client.authorize_url(**params).gsub(/api-ssl\./,'')
end