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. 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 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 may simplify development as they require less configuration than other authentication methods. There are some risks associated with API keys, we recommended you 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 simplify the rest of the example: