|
| 1 | +# Authentication |
| 2 | + |
| 3 | +The recommended way to authenticate to the google-ads-data_manager library is to use |
| 4 | +[Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/application-default-credentials). |
| 5 | +To review all of your authentication options, see [Credentials lookup](#credential-lookup). |
| 6 | + |
| 7 | +## Quickstart |
| 8 | + |
| 9 | +The following example shows how to set up authentication for a local development |
| 10 | +environment with your user credentials. |
| 11 | + |
| 12 | +**NOTE:** This method is _not_ recommended for running in production. User credentials |
| 13 | +should be used only during development. |
| 14 | + |
| 15 | +1. [Download and install the Google Cloud CLI](https://cloud.google.com/sdk). |
| 16 | +2. Set up a local ADC file with your user credentials: |
| 17 | + |
| 18 | +```sh |
| 19 | +gcloud auth application-default login |
| 20 | +``` |
| 21 | + |
| 22 | +3. Write code as if already authenticated. |
| 23 | + |
| 24 | +For more information about setting up authentication for a local development environment, see |
| 25 | +[Set up Application Default Credentials](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-dev). |
| 26 | + |
| 27 | +## Credential Lookup |
| 28 | + |
| 29 | +The google-ads-data_manager library provides several mechanisms to configure your system. |
| 30 | +Generally, using Application Default Credentials to facilitate automatic |
| 31 | +credentials discovery is the easist method. But if you need to explicitly specify |
| 32 | +credentials, there are several methods available to you. |
| 33 | + |
| 34 | +Credentials are accepted in the following ways, in the following order or precedence: |
| 35 | + |
| 36 | +1. Credentials specified in method arguments |
| 37 | +2. Credentials specified in configuration |
| 38 | +3. Credentials pointed to or included in environment variables |
| 39 | +4. Credentials found in local ADC file |
| 40 | +5. Credentials returned by the metadata server for the attached service account (GCP) |
| 41 | + |
| 42 | +### Configuration |
| 43 | + |
| 44 | +You can configure a path to a JSON credentials file, either for an individual client object or |
| 45 | +globally, for all client objects. The JSON file can contain credentials created for |
| 46 | +[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation), |
| 47 | +[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a |
| 48 | +[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key). |
| 49 | + |
| 50 | +Note: Service account keys are a security risk if not managed correctly. You should |
| 51 | +[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree) |
| 52 | +whenever possible. |
| 53 | + |
| 54 | +To configure a credentials file for an individual client initialization: |
| 55 | + |
| 56 | +```ruby |
| 57 | +require "google/ads/data_manager" |
| 58 | + |
| 59 | +client = Google::Ads::DataManager.ingestion_service do |config| |
| 60 | + config.credentials = "path/to/credentialfile.json" |
| 61 | +end |
| 62 | +``` |
| 63 | + |
| 64 | +To configure a credentials file globally for all clients: |
| 65 | + |
| 66 | +```ruby |
| 67 | +require "google/ads/data_manager" |
| 68 | + |
| 69 | +Google::Ads::DataManager.configure do |config| |
| 70 | + config.credentials = "path/to/credentialfile.json" |
| 71 | +end |
| 72 | + |
| 73 | +client = Google::Ads::DataManager.ingestion_service |
| 74 | +``` |
| 75 | + |
| 76 | +### Environment Variables |
| 77 | + |
| 78 | +You can also use an environment variable to provide a JSON credentials file. |
| 79 | +The environment variable can contain a path to the credentials file or, for |
| 80 | +environments such as Docker containers where writing files is not encouraged, |
| 81 | +you can include the credentials file itself. |
| 82 | + |
| 83 | +The JSON file can contain credentials created for |
| 84 | +[workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation), |
| 85 | +[workforce identity federation](https://cloud.google.com/iam/docs/workforce-identity-federation), or a |
| 86 | +[service account key](https://cloud.google.com/docs/authentication/provide-credentials-adc#local-key). |
| 87 | + |
| 88 | +Note: Service account keys are a security risk if not managed correctly. You should |
| 89 | +[choose a more secure alternative to service account keys](https://cloud.google.com/docs/authentication#auth-decision-tree) |
| 90 | +whenever possible. |
| 91 | + |
| 92 | +The environment variables that google-ads-data_manager |
| 93 | +checks for credentials are: |
| 94 | + |
| 95 | +* `GOOGLE_CLOUD_CREDENTIALS` - Path to JSON file, or JSON contents |
| 96 | +* `GOOGLE_APPLICATION_CREDENTIALS` - Path to JSON file |
| 97 | + |
| 98 | +```ruby |
| 99 | +require "google/ads/data_manager" |
| 100 | + |
| 101 | +ENV["GOOGLE_APPLICATION_CREDENTIALS"] = "path/to/credentialfile.json" |
| 102 | + |
| 103 | +client = Google::Ads::DataManager.ingestion_service |
| 104 | +``` |
| 105 | + |
| 106 | +### Local ADC file |
| 107 | + |
| 108 | +You can set up a local ADC file with your user credentials for authentication during |
| 109 | +development. If credentials are not provided in code or in environment variables, |
| 110 | +then the local ADC credentials are discovered. |
| 111 | + |
| 112 | +Follow the steps in [Quickstart](#quickstart) to set up a local ADC file. |
| 113 | + |
| 114 | +### Google Cloud Platform environments |
| 115 | + |
| 116 | +When running on Google Cloud Platform (GCP), including Google Compute Engine |
| 117 | +(GCE), Google Kubernetes Engine (GKE), Google App Engine (GAE), Google Cloud |
| 118 | +Functions (GCF) and Cloud Run, credentials are retrieved from the attached |
| 119 | +service account automatically. Code should be written as if already authenticated. |
| 120 | + |
| 121 | +For more information, see |
| 122 | +[Set up ADC for Google Cloud services](https://cloud.google.com/docs/authentication/provide-credentials-adc#attached-sa). |
0 commit comments