This document describes how to override the default authentication used by Rust. The Cloud Client Libraries for Rust automatically authenticate your requests to Google Cloud services, but you might occasionally need to override your default credentials.
Prerequisites
This document uses the Cloud Natural Language API. To install this API, follow the service quickstart, which shows you how to enable a service.
For complete setup instructions for the Rust libraries, see Getting started with Rust.
Dependencies
You must declare the dependencies in your Cargo.toml file:
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 for information about how you can configure this default without any code changes in your application.
Add some use declarations to simplify the rest of the example:
Initialize the client using the defaults:
Use this client as usual:
Override the default credentials: API keys
API keys are text strings that grant access to some Google Cloud services. Using API keys might simplify development as they require less configuration than other authentication methods. API keys carry security risks. Read Best practices for managing API keys if you plan to use them.
Write a function that receives the API key string as an input parameter:
Add some use declarations to simplify the rest of the example:
Use the API Keys Builder to create the credentials:
Initialize the client using the
credentialsobject:And use this client as usual:
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 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). You then request short-lived credentials for a service account that has the authorization your use case requires.
It's more secure than downloading a service account key for the target service account. You don't need to hold the credentials in the file system or in memory.
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.
Add some use declarations to simplify the rest of the example:
Use the impersonated service account Builder to create the credentials:
Initialize the client using the
credentialsobject:And use this client as usual:
More information
Learn about other authentication methods in the Rust client libraries:
- Anonymous Credentials: to access services or resources that don't require authentication.
- External Accounts: to use Workload identity federation with the Rust client libraries.
- Service Accounts: to initialize credentials from a service account key.