Class: Bitly::OAuth
- Inherits:
-
Object
- Object
- Bitly::OAuth
- 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
-
#client_id ⇒ Object
readonly
Returns the value of attribute client_id.
-
#client_secret ⇒ Object
readonly
Returns the value of attribute client_secret.
Instance Method Summary collapse
-
#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.
-
#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.
-
#initialize(client_id:, client_secret:) ⇒ Bitly::OAuth
constructor
Creates a new Bitly::OAuth client to retrieve user access tokens.
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
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_id ⇒ Object (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_secret ⇒ Object (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.
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.
57 58 59 60 61 62 63 64 |
# File 'lib/bitly/oauth.rb', line 57 def (redirect_uri, state: nil) params = { redirect_uri: redirect_uri, client_id: client_id } params[:state] = state if state @client.(**params).gsub(/api-ssl\./,'') end |