This document describes how to override the default authentication used by
Rust. The Rust automatically authenticate your
requests to Google Cloud services, but you may occasionally need to override
your default credentials.

## Prerequisites

This guide uses the [Cloud Natural Language API](https://cloud.google.com/natural-language). To install this API follow
the [service quickstart](https://cloud.google.com/natural-language/docs/setup), which shows you how to enable a service.

For complete setup instructions for the Rust libraries, see [Getting started
with Rust](https://docs.cloud.google.com/rust/quickstart).

### Dependencies

You must declare the dependencies in your `Cargo.toml` file:

```bash
cargo add google-cloud-language-v2 google-cloud-auth
```

## The default credentials

The recommended way to authenticate applications in Google Cloud is to use
Application Default Credentials, without any configuration the client libraries
default to this credential type. See [How Application Default Credentials work](https://cloud.google.com/docs/authentication/application-default-credentials)
for information about you can configure this default without any code changes in
your application.

1. Add some use declarations to simplify the rest of the example:


   ```rust
   use google_cloud_language_v2::client::LanguageService;
   use google_cloud_language_v2::model::{Document, document::Type};
   ```

   <br />

2. Initialize the client using the defaults:


   ```rust
   let client = LanguageService::builder().build().await?;
   ```

   <br />

3. Use this client as usual:


   ```rust
   let response = client
       .analyze_sentiment()
       .set_document(
           Document::new()
               .set_content("Hello World!")
               .set_type(Type::PlainText),
       )
       .send()
       .await?;
   println!("response={response:?}");
   ```

   <br />

## Override the default credentials: API keys

[API keys](https://cloud.google.com/docs/authentication/api-keys) are text strings that grant access to some Google Cloud services.
Using API keys may simplify development as they require less configuration than
other [authentication methods](https://cloud.google.com/docs/authentication). There are some risks associated with API keys,
we recommended you read [Best practices for managing API keys](https://cloud.google.com/docs/authentication/api-keys-best-practices) if you plan to
use them.

1. Write a function that receives the API key string as an input parameter:


   ```rust
   pub async fn sample(key: &str) -> anyhow::Result<()> {
   ```

   <br />

2. Add some use declarations simplify the rest of the example:


   ```rust
   use google_cloud_auth::credentials::api_key_credentials::Builder as ApiKeyCredentialsBuilder;
   use google_cloud_language_v2::client::LanguageService;
   use google_cloud_language_v2::model::{Document, document::Type};
   ```

   <br />

3. Use the API Keys [Builder](https://docs.rs/google-cloud-auth/latest/google_cloud_auth/credentials/api_key_credentials/struct.Builder.html) to create the credentials:


   ```rust
   let credentials = ApiKeyCredentialsBuilder::new(key).build();
   ```

   <br />

4. Initialize the client using the `credentials` object:


   ```rust
   let client = LanguageService::builder()
       .with_credentials(credentials)
       .build()
       .await?;
   ```

   <br />

5. And use this client as usual:


   ```rust
   let response = client
       .analyze_sentiment()
       .set_document(
           Document::new()
               .set_content("Hello World!")
               .set_type(Type::PlainText),
       )
       .send()
       .await?;
   println!("response={response:?}");
   ```

   <br />

## Override the default credentials: service account impersonation

Service account impersonation lets you make API calls on behalf of a
service account. [Use service account impersonation](https://cloud.google.com/docs/authentication/use-service-account-impersonation) discusses this form of
authentication in detail.

When you use service account impersonation, you start with an authenticated
principal (your user account or a service account) and request short-lived
credentials for a service account that has the authorization that your use case
requires.

It is more secure than downloading a service account key for the target service
account, as you don't need to hold the credentials in the file system or even
in memory.

1. Write the example in a function that receives the service account identifier
   as an input parameter. This can be the service account email or the unique
   numeric Google-assigned identifier of the service account.


   ```rust
   /// # Parameters
   /// * `target_principal`: the email or unique id of the target service account.
   ///   For example: `my-service-account@my-project.iam.gserviceaccount.com`.
   pub async fn sample(target_principal: &str) -> anyhow::Result<()> {
   ```

   <br />

2. Add some use declarations simplify the rest of the example:


   ```rust
   use google_cloud_auth::credentials::Builder as AdcCredentialsBuilder;
   use google_cloud_auth::credentials::impersonated::Builder as ImpersonatedCredentialsBuilder;
   use google_cloud_language_v2::client::LanguageService;
   use google_cloud_language_v2::model::{Document, document::Type};
   ```

   <br />

3. Use the impersonated service account
   [Builder](https://docs.rs/google-cloud-auth/latest/google_cloud_auth/credentials/impersonated/struct.Builder.html)
   to create the credentials:


   ```rust
   let credentials = ImpersonatedCredentialsBuilder::from_source_credentials(
       AdcCredentialsBuilder::default().build()?,
   )
   .with_target_principal(target_principal)
   .build()?;
   ```

   <br />

4. Initialize the client using the `credentials` object:


   ```rust
   let client = LanguageService::builder()
       .with_credentials(credentials)
       .build()
       .await?;
   ```

   <br />

5. And use this client as usual:


   ```rust
   let response = client
       .analyze_sentiment()
       .set_document(
           Document::new()
               .set_content("Hello World!")
               .set_type(Type::PlainText),
       )
       .send()
       .await?;
   println!("response={response:?}");
   ```

   <br />

## More Information

Learn about other authentication methods in the Rust client libraries:

- [Anonymous Credentials](https://docs.rs/google-cloud-auth/latest/google_cloud_auth/credentials/anonymous/struct.Builder.html): to access services or resources that don't require authentication.
- [External Accounts](https://docs.rs/google-cloud-auth/latest/google_cloud_auth/credentials/external_account/struct.Builder.html): to use [Workload identity federation](https://cloud.google.com/iam/docs/workload-identity-federation) with the Rust client libraries.
- [Service Accounts](https://docs.rs/google-cloud-auth/latest/google_cloud_auth/credentials/impersonated/struct.Builder.html): to initialize credentials from a [service account key](https://cloud.google.com/iam/docs/service-account-creds#key-types).