If you are running your application outside of Google Cloud, you need to provide credentials that are recognized by Google Cloud to use Google Cloud services.

<br />

### Workload Identity Federation

The preferred way to authenticate with Google Cloud using credentials from
an external IdP is to use [Workload Identity Federation](https://docs.cloud.google.com/iam/docs/workload-identity-federation.md.txt);
you create a credential configuration file and set the
`GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to it. This
approach is more secure than creating a service account key.

For help with setting up Workload Identity Federation for ADC, see
[Workload Identity Federation with other clouds](https://docs.cloud.google.com/iam/docs/workload-identity-federation-with-other-clouds.md.txt).

### Service account key

If you are not able to configure Workload Identity Federation, then you must
create a service account, grant it the IAM roles that
your application requires, and create a key for the service account.


> [!NOTE]
> **Note:** Service account keys are a security risk if not managed correctly. You should [choose a more secure alternative to service account keys](https://docs.cloud.google.com/docs/authentication.md.txt#auth-decision-tree) whenever possible. If you must authenticate with a service account key, you are responsible for the security of the private key and for other operations described by [Best practices for managing service account keys](https://docs.cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys.md.txt). If you are prevented from creating a service account key, service account key creation might be disabled for your organization. For more information, see [Managing secure-by-default organization resources](https://docs.cloud.google.com/resource-manager/docs/secure-by-default-organizations).
>
>
> If you acquired the service account key from an external source, you must validate it before use.
> For more information, see [Security requirements for externally sourced credentials](https://docs.cloud.google.com/docs/authentication/external/externally-sourced-credentials).

To create a service account key and make it available to ADC:

1. Create a service account with the roles your application needs, and a key for that service account, by following the instructions in [Creating a service account key](https://docs.cloud.google.com/iam/docs/keys-create-delete.md.txt#creating).
2.
   Set the environment variable `GOOGLE_APPLICATION_CREDENTIALS`
   to the path of the JSON file that contains your credentials.
   This variable applies only to your current shell session, so if you open
   a new session, set the variable again.

   **Example:** Linux or macOS

   ```bash
   export GOOGLE_APPLICATION_CREDENTIALS="`KEY_PATH`"
   ```

   Replace `KEY_PATH` with the path of the JSON file that contains your credentials.

   For example:

   ```bash
   export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
   ```
   **Example:** Windows

   For PowerShell:

   ```bash
   $env:GOOGLE_APPLICATION_CREDENTIALS="`KEY_PATH`"
   ```

   Replace `KEY_PATH` with the path of the JSON file that contains your credentials.

   For example:

   ```bash
   $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
   ```

   For command prompt:

   ```bash
   set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH
   ```

   Replace `KEY_PATH` with the path of the JSON file that contains your credentials.

> [!NOTE]
> **Note:** When you set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable, ADC checks this location first, then checks other locations only if necessary.

<br />

## What's next

- Learn about [Workload Identity Federation](https://docs.cloud.google.com/iam/docs/workload-identity-federation.md.txt).
- Understand best practices for using [service account keys](https://docs.cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys.md.txt).
- Learn more about [how ADC finds credentials](https://docs.cloud.google.com/docs/authentication/application-default-credentials.md.txt).
- Explore [authentication methods](https://docs.cloud.google.com/docs/authentication.md.txt).