class BingAdsRubySdk::OAuth2::AuthorizationHandler
Adds some useful methods to Signet::OAuth2::Client
Attributes
Public Class Methods
@param developer_token @param client_id @param store [Store]
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 12 def initialize(developer_token:, client_id:, store:, client_secret: nil) @client = Signet::OAuth2::Client.new( client_params(developer_token, client_id, client_secret) ) @store = store refresh_from_store end
Public Instance Methods
@return [String] unless client.client_id url is nil interpolated url. @return [nil] if client.client_id is nil.
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 22 def code_url return nil if client.client_id.nil? "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=#{client.client_id}&"\ "scope=offline_access+https://ads.microsoft.com/ads.manage&response_type=code&"\ "redirect_uri=https://login.microsoftonline.com/common/oauth2/nativeclient" end
Once you have completed the oauth process in your browser using the code_url
copy the url your browser has been redirected to and use it as argument here
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 31 def fetch_from_url(url) codes = extract_codes(url) return false if codes.none? fetch_from_code(codes.last) rescue Signet::AuthorizationError, URI::InvalidURIError false end
Get or fetch an access token. @return [String] The access token.
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 42 def fetch_or_refresh if client.expired? client.refresh! store.write(token_data) end client.access_token end
Private Instance Methods
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 78 def client_params(developer_token, client_id, client_secret) { authorization_uri: 'https://login.microsoftonline.com/common/oauth2/v2.0/authorize', token_credential_uri: 'https://login.microsoftonline.com/common/oauth2/v2.0/token', redirect_uri: 'https://login.microsoftonline.com/common/oauth2/nativeclient', developer_token: developer_token, client_id: client_id, scope: 'offline_access' }.tap do |hash| hash[:client_secret] = client_secret if client_secret end end
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 72 def extract_codes(url) url = URI.parse(url) query_params = URI.decode_www_form(url.query) query_params.find { |arg| arg.first.casecmp("CODE").zero? } end
Request the Api
to exchange the code for the access token. Save the access token through the store. @param [String] code authorization code from bing's ads. @return [#store.write] store's write output.
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 66 def fetch_from_code(code) client.code = code client.fetch_access_token! store.write(token_data) end
Refresh existing authorization token @return [Signet::OAuth2::Client] if everything went well. @return [nil] if the token can't be read from the store.
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 57 def refresh_from_store ext_token = store.read client.update_token!(ext_token) if ext_token end
# File lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb, line 91 def token_data { access_token: client.access_token, refresh_token: client.refresh_token, issued_at: client.issued_at, expires_in: client.expires_in } end