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:) ⇒ AuthorizationHandler

Returns a new instance of AuthorizationHandler.

Parameters:

  • developer_token
  • client_id
  • store (Store)


12
13
14
15
16
# File 'lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb', line 12

def initialize(developer_token:, client_id:, store:)
  @client = build_client(developer_token, client_id)
  @store  = store
  refresh_from_store
end

Instance Method Details

#code_urlString?

Returns:

  • (String)

    unless client.client_id url is nil interpolated url.

  • (nil)

    if client.client_id is nil.



20
21
22
23
# File 'lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb', line 20

def code_url
  return nil if client.client_id.nil?
  "https://login.live.com/oauth20_authorize.srf?client_id=#{client.client_id}&scope=bingads.manage&response_type=code&redirect_uri=https://login.live.com/oauth20_desktop.srf"
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



27
28
29
30
31
32
33
34
# File 'lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb', line 27

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.

Returns:

  • (String)

    The access token.



38
39
40
41
42
43
44
# File 'lib/bing_ads_ruby_sdk/oauth2/authorization_handler.rb', line 38

def fetch_or_refresh
  if client.expired?
    client.refresh!
    store.write(token_data)
  end
  client.access_token
end