Skip to content

Commit a96dc74

Browse files
authored
Add ADC support (#532)
* Add ADC support Change-Id: I30498dc41703428ee179c104d783e19bb12abb69 * Reorder configs to keep service account fields together Change-Id: Ic55dfa9d3a45b8bef88ab38d34d278d21f8b1757 * Rename env variable Change-Id: I5b3aea4c0da6be73884a532145ea879b8bfba4f3 * Prioritize the use_application_default_credentials flag Change-Id: I2eed7176be117abbd47be64871cda3e69636061e
1 parent e3a2cd7 commit a96dc74

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ gem 'yard', '~> 0.9.27'
1313
gem 'rubocop', require: false
1414
gem 'rubocop-google_ads', require: false
1515
gem 'grpc', ['~> 1.0', '!= 1.74.0']
16+
gem 'googleauth', '~> 1.15.1'

google_ads_config.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
c.client_secret = 'INSERT_CLIENT_SECRET_HERE'
2727
c.refresh_token = 'INSERT_REFRESH_TOKEN_HERE'
2828

29+
# You can also authenticate using Application Default Credentials (ADC)
30+
# To understand how ADC discovers credentials in a given environment,
31+
# see: https://developers.google.com/identity/protocols/application-default-credentials.
32+
c.use_application_default_credentials = false
33+
2934
# Whether to use the Google Cloud Organization of your Google Cloud
3035
# project instead of developer token to determine your Google Ads API access levels.
3136
# Use this flag only if you are enrolled into a limited pilot that supports

lib/google/ads/google_ads/config.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Config
2727
attr_accessor :client_secret
2828
attr_accessor :keyfile
2929
attr_accessor :impersonate
30+
attr_accessor :use_application_default_credentials
3031
attr_accessor :authentication
3132

3233
attr_accessor :developer_token
@@ -51,6 +52,7 @@ def initialize(&block)
5152
@client_secret = nil
5253
@keyfile = nil
5354
@impersonate = nil
55+
@use_application_default_credentials = nil
5456
@authentication = nil
5557

5658
@developer_token = nil

lib/google/ads/google_ads/google_ads_client.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
require 'json'
3636
require 'openssl'
3737
require 'signet/oauth_2/client'
38+
require 'googleauth'
3839
require 'delegate'
3940

4041
module Google
@@ -100,6 +101,7 @@ def load_environment_config
100101
@config.client_secret = ENV.fetch("GOOGLE_ADS_CLIENT_SECRET", @config.client_secret)
101102
@config.keyfile = ENV.fetch("GOOGLE_ADS_JSON_KEY_FILE_PATH", @config.keyfile)
102103
@config.impersonate = ENV.fetch("GOOGLE_ADS_IMPERSONATED_EMAIL", @config.impersonate)
104+
@config.use_application_default_credentials = ENV.fetch("GOOGLE_ADS_USE_APPLICATION_DEFAULT_CREDENTIALS", @config.use_application_default_credentials)
103105
@config.developer_token = ENV.fetch("GOOGLE_ADS_DEVELOPER_TOKEN", @config.developer_token)
104106
@config.login_customer_id = ENV.fetch("GOOGLE_ADS_LOGIN_CUSTOMER_ID", @config.login_customer_id)
105107
@config.linked_customer_id = ENV.fetch("GOOGLE_ADS_LINKED_CUSTOMER_ID", @config.linked_customer_id)
@@ -201,7 +203,9 @@ def decode_warning(warning)
201203
private
202204

203205
def get_credentials
204-
if @config.authentication
206+
if @config.use_application_default_credentials
207+
get_application_default_credentials
208+
elsif @config.authentication
205209
@config.authentication
206210
elsif @config.keyfile
207211
get_service_account_credentials
@@ -236,6 +240,12 @@ def get_service_account_credentials
236240
scope: [SCOPE],
237241
).updater_proc
238242
end
243+
244+
# Provides a Google::Auth::Credentials initialized with Application
245+
# Default Credentials specified in the config.
246+
def get_application_default_credentials
247+
Google::Auth.get_application_default(SCOPE).updater_proc
248+
end
239249

240250
# Create the default logger, useful if the user hasn't defined one.
241251
def create_default_logger()

0 commit comments

Comments
 (0)