Module: Dnsimple::Client::Oauth
- Included in:
- OauthService
- Defined in:
- lib/dnsimple/client/oauth.rb
Instance Method Summary collapse
-
#authorize_url(client_id, options = {}) ⇒ String
Gets the URL to authorize an user for an application via the OAuth2 flow.
-
#exchange_authorization_for_token(code, client_id, client_secret, options = {}) ⇒ String
Exchange the short-lived authorization code for an access token you can use to authenticate your API calls.
Instance Method Details
#authorize_url(client_id, options = {}) ⇒ String
Gets the URL to authorize an user for an application via the OAuth2 flow.
38 39 40 41 42 43 44 45 46 47 |
# File 'lib/dnsimple/client/oauth.rb', line 38 def (client_id, = {}) site_url = client.base_url.sub("api.", "") url = URI.join(site_url, "/oauth/authorize?client_id=#{client_id}") = .merge(response_type: "code") .each do |key, value| url.query += "&#{key}=#{value}" end url.to_s end |
#exchange_authorization_for_token(code, client_id, client_secret, options = {}) ⇒ String
Exchange the short-lived authorization code for an access token you can use to authenticate your API calls.
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dnsimple/client/oauth.rb', line 16 def (code, client_id, client_secret, = {}) attributes = { code: code, client_id: client_id, client_secret: client_secret, grant_type: "authorization_code" } attributes[:state] = .delete(:state) if .key?(:state) attributes[:redirect_uri] = .delete(:redirect_uri) if .key?(:redirect_uri) response = client.post(Client.versioned("/oauth/access_token"), attributes, ) Struct::OauthToken.new(response) rescue Dnsimple::RequestError => exception raise exception unless exception.http_response.code == 400 raise Dnsimple::OAuthInvalidRequestError, exception.http_response end |