Skip to content

Commit

Permalink
Support for use_cloud_org_for_api_access
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfrg committed Nov 14, 2023
1 parent 94b3b76 commit df8e03a
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
6 changes: 6 additions & 0 deletions google_ads_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
c.client_secret = 'INSERT_CLIENT_SECRET_HERE'
c.refresh_token = 'INSERT_REFRESH_TOKEN_HERE'

# Whether to use the Google Cloud Organization of your Google Cloud
# project instead of developer token to determine your Google Ads API access levels.
# Use this flag only if you are enrolled into a limited pilot that supports
# this configuration.
# c.use_cloud_org_for_api_access = false

# You can also authenticate using a service account. If "keyfile" is
# specified below, then service account authentication will be assumed and
# the above authentication fields ignored. Read more about service account
Expand Down
2 changes: 2 additions & 0 deletions lib/google/ads/google_ads/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Config
attr_accessor :developer_token
attr_accessor :login_customer_id
attr_accessor :linked_customer_id
attr_accessor :use_cloud_org_for_api_access

attr_accessor :log_level
attr_accessor :log_target
Expand All @@ -55,6 +56,7 @@ def initialize(&block)
@developer_token = nil
@login_customer_id = nil
@linked_customer_id = nil
@use_cloud_org_for_api_access = false

@log_level = nil
@log_target = nil
Expand Down
1 change: 1 addition & 0 deletions lib/google/ads/google_ads/google_ads_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def load_environment_config
@config.developer_token = ENV.fetch("GOOGLE_ADS_DEVELOPER_TOKEN", @config.developer_token)
@config.login_customer_id = ENV.fetch("GOOGLE_ADS_LOGIN_CUSTOMER_ID", @config.login_customer_id)
@config.linked_customer_id = ENV.fetch("GOOGLE_ADS_LINKED_CUSTOMER_ID", @config.linked_customer_id)
@config.use_cloud_org_for_api_access = ENV.fetch("GOOGLE_ADS_USE_CLOUD_ORG_FOR_API_ACCESS", @config.use_cloud_org_for_api_access)
@config.api_endpoint = ENV.fetch("GOOGLE_ADS_ENDPOINT", @config.api_endpoint)

# Client library-specific variables
Expand Down
10 changes: 7 additions & 3 deletions lib/google/ads/google_ads/service_lookup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,13 @@ def gax_service_params
end

def headers
headers = {
:"developer-token" => config.developer_token
}
headers = {}

# If config.use_cloud_org_for_api_access is not True, add the developer
# token to the request's metadata
if !config.use_cloud_org_for_api_access
headers[:"developer-token"] = config.developer_token
end

if config.login_customer_id
validate_customer_id(:login_customer_id)
Expand Down
22 changes: 22 additions & 0 deletions test/test_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,26 @@ def test_configure()
assert_equal(client_secret_value, config.client_secret)
assert_equal(developer_token_value, config.developer_token)
end

def test_use_cloud_org_for_api_access()
config = Google::Ads::GoogleAds::Config.new

refresh_token_value = '1234'
client_id_value = 'abcd'
client_secret_value = '!@#$'
use_cloud_org_for_api_access = true

config.configure do |c|
c.refresh_token = refresh_token_value
c.client_id = client_id_value
c.client_secret = client_secret_value
c.use_cloud_org_for_api_access = true
end

assert_equal(refresh_token_value, config.refresh_token)
assert_equal(client_id_value, config.client_id)
assert_equal(client_secret_value, config.client_secret)
assert_equal(nil, config.developer_token)
assert_equal(true, config.use_cloud_org_for_api_access)
end
end

0 comments on commit df8e03a

Please sign in to comment.