Class: BingAdsRubySdk::OAuth2::AuthorizationHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb

Overview

Adds some useful methods to Signet::OAuth2::Client

Instance Method Summary collapse

Constructor Details

#initialize(developer_token:, client_id:, store:, client_secret: nil) ⇒ AuthorizationHandler



12
13
14
15
16
17
18
# 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

Instance Method Details

#code_urlString?



22
23
24
25
26
27
# 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

#fetch_from_url(url) ⇒ Object

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



31
32
33
34
35
36
37
38
# 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

#fetch_or_refreshString

Get or fetch an access token.



42
43
44
45
46
47
48
# 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