Most services in Google Cloud Platform require the client to authenticate the requests. Notable exceptions include public buckets in GCS and public data sets in BigQuery. The C++ client libraries are automatically configured to useApplication Default Credentials, but some applications may need to override this default. The functions and classes related to changing the authentication configuration are documented here.
This document is not a general introduction to authentication for Google Cloud Platform. For readers seeking such an introduction we recommendAuthentication at Googleas a good starting point. Covering authorization in any detail is also out of scope. We recommend reading theIAM overviewif that is of interest.
The most common motivation to change authentication is to use a differentprincipal, that is, to make the request(s) on behalf of a different user or robot account. In many cases the default principal can be changed without having to change any code. TheApplication Default Credentialscan be configured via environment variables, or via thegcloudCLI. If your application is running in GCE, Cloud Run, GKE or a similar environment you or the system administrator may change the service account associated with this deployment environment. Consider using these mechanisms instead of changing the code, as that may give your DevOps, SRE, or system administration team more flexibility.
General Concepts
While a complete overview of authentication and authorization for Google Cloud is outside the scope of this document, a brief introduction may help.
As we mentioned in passing, authentication supports both user accounts and service accounts. These are referred as "principals". User accounts represent a developer, administrator, or any other person who interacts with GCP services. In contrast, service accounts are accounts that do not represent a person. They typically represent an application, or another service.
Google Cloud Platform largely usesOAuth2access tokens for authentication. There are multiple ways to create such tokens. For example, when running on GCE the VM has access to a metadata server that can create these tokens for any application running on the VM. As another example, you can download aservice account keyfileand the C++ client libraries will create access tokens using the contents of this file.
Access tokens are bearer tokens. Having the token authenticates the principal, they can be used in separate connections, or from different computers than the one used to create them. If these tokens are exposed to third-parties the token can be used to make calls on behalf of the principal identified by the token. To reduce exposure, access tokens are always time limited. They automatically expire after a period of time (usually one hour).
The client libraries automatically refresh access tokens, that is, create new tokens before they expire. The only exception isMakeAccessTokenCredentials(), where the application provides the access token.
The client libraries also support authentication without a principal via API keys. Note that while the client libraries support sending the API key, not all Google Cloud Platform services support this method of authentication.
Development Workstations
We find that developers typically use Application Default Credentials to test their applications. The developer would either:
Usegcloud auth application-defaultto authenticate using the developer's account. Keep in mind that this persists a "refresh token" in your workstation's filesystem. This refresh token is long-lived and can be used to create access tokens with full access to all GCP services.
Set theGOOGLE_APPLICATION_CREDENTIALSenvironment variable to load a service account key. The value of this environment variable is the full path of a file that contains the service account key. Keep in mind the security implications of keeping such a file as plain text in your filesystem.
If you are using a GCE instance as your development environment, simply use the service account of the GCE machine to access GCP services.
Limitations
The C++ authentication components do not allow applications to create their own credential types. It is not possible to extend the C++ libraries without changing internal components. If you need additional functionality please file afeature requeston GitHub. Likewise, creating the components that implement (as opposed todescribing) authentication flows are also considered implementation details. If you would like to use them in your own libraries please file afeature request. We cannot promise that we will be able to satisfy these requests, but we will give them full consideration.
Important**: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud Platform, you must validate it before providing it to any Google API or client library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information, refer toexternally sourced credentials.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThe latest version of the C++ client library is 2.37.0-rc, while a range of previous versions from 2.36.0 down to 2.10.1 are also accessible.\u003c/p\u003e\n"],["\u003cp\u003eThe C++ client libraries support authentication via Application Default Credentials, which can be configured through environment variables or the \u003ccode\u003egcloud\u003c/code\u003e CLI without changing code.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Cloud Platform primarily uses OAuth2 access tokens for authentication, and the C++ client libraries automatically handle token refreshing, with the exception of \u003ccode\u003eMakeAccessTokenCredentials()\u003c/code\u003e which requires an externally provided token.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can use Application Default Credentials, set the \u003ccode\u003eGOOGLE_APPLICATION_CREDENTIALS\u003c/code\u003e environment variable, or use a GCE instance's service account for authentication during development.\u003c/p\u003e\n"],["\u003cp\u003eIf you accept credentials from an external source, you must validate it before providing it to any Google API or client library.\u003c/p\u003e\n"]]],[],null,["# Authentication Components (2.42.0-rc)\n\nVersion latestkeyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/common/latest/group__guac)\n- [2.41.0](/cpp/docs/reference/common/2.41.0/group__guac)\n- [2.40.0](/cpp/docs/reference/common/2.40.0/group__guac)\n- [2.39.0](/cpp/docs/reference/common/2.39.0/group__guac)\n- [2.38.0](/cpp/docs/reference/common/2.38.0/group__guac)\n- [2.37.0](/cpp/docs/reference/common/2.37.0/group__guac)\n- [2.36.0](/cpp/docs/reference/common/2.36.0/group__guac)\n- [2.35.0](/cpp/docs/reference/common/2.35.0/group__guac)\n- [2.34.0](/cpp/docs/reference/common/2.34.0/group__guac)\n- [2.33.0](/cpp/docs/reference/common/2.33.0/group__guac)\n- [2.32.0](/cpp/docs/reference/common/2.32.0/group__guac)\n- [2.31.0](/cpp/docs/reference/common/2.31.0/group__guac)\n- [2.30.0](/cpp/docs/reference/common/2.30.0/group__guac)\n- [2.29.0](/cpp/docs/reference/common/2.29.0/group__guac)\n- [2.28.0](/cpp/docs/reference/common/2.28.0/group__guac)\n- [2.27.0](/cpp/docs/reference/common/2.27.0/group__guac)\n- [2.26.0](/cpp/docs/reference/common/2.26.0/group__guac)\n- [2.25.1](/cpp/docs/reference/common/2.25.1/group__guac)\n- [2.24.0](/cpp/docs/reference/common/2.24.0/group__guac)\n- [2.23.0](/cpp/docs/reference/common/2.23.0/group__guac)\n- [2.22.1](/cpp/docs/reference/common/2.22.1/group__guac)\n- [2.21.0](/cpp/docs/reference/common/2.21.0/group__guac)\n- [2.20.0](/cpp/docs/reference/common/2.20.0/group__guac)\n- [2.19.0](/cpp/docs/reference/common/2.19.0/group__guac)\n- [2.18.0](/cpp/docs/reference/common/2.18.0/group__guac)\n- [2.17.0](/cpp/docs/reference/common/2.17.0/group__guac)\n- [2.16.0](/cpp/docs/reference/common/2.16.0/group__guac)\n- [2.15.1](/cpp/docs/reference/common/2.15.1/group__guac)\n- [2.14.0](/cpp/docs/reference/common/2.14.0/group__guac)\n- [2.13.0](/cpp/docs/reference/common/2.13.0/group__guac)\n- [2.12.0](/cpp/docs/reference/common/2.12.0/group__guac)\n- [2.11.0](/cpp/docs/reference/common/2.11.0/group__guac)\n- [2.10.1](/cpp/docs/reference/common/2.10.1/group__guac) \n\nAuthentication Components\n=========================\n\n### Overview and Motivation\n\nMost services in Google Cloud Platform require the client to authenticate the requests. Notable exceptions include public buckets in GCS and public data sets in BigQuery. The C++ client libraries are automatically configured to use [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials), but some applications may need to override this default. The functions and classes related to changing the authentication configuration are documented here.\n\nThis document is not a general introduction to authentication for Google Cloud Platform. For readers seeking such an introduction we recommend [Authentication at Google](https://cloud.google.com/docs/authentication) as a good starting point. Covering authorization in any detail is also out of scope. We recommend reading the [IAM overview](https://cloud.google.com/iam/docs/overview) if that is of interest.\n\nThe most common motivation to change authentication is to use a different [principal](https://cloud.google.com/iam/docs/overview#how_cloud_iam_works), that is, to make the request(s) on behalf of a different user or robot account. In many cases the default principal can be changed without having to change any code. The [Application Default Credentials](https://cloud.google.com/docs/authentication/application-default-credentials) can be configured via environment variables, or via the `gcloud` CLI. If your application is running in GCE, Cloud Run, GKE or a similar environment you or the system administrator may change the service account associated with this deployment environment. Consider using these mechanisms instead of changing the code, as that may give your DevOps, SRE, or system administration team more flexibility.\n\n### General Concepts\n\nWhile a complete overview of authentication and authorization for Google Cloud is outside the scope of this document, a brief introduction may help.\n\nAs we mentioned in passing, authentication supports both user accounts and service accounts. These are referred as \"principals\". User accounts represent a developer, administrator, or any other person who interacts with GCP services. In contrast, service accounts are accounts that do not represent a person. They typically represent an application, or another service.\n\nGoogle Cloud Platform largely uses [OAuth2](https://oauth.net/2/) access tokens for authentication. There are multiple ways to create such tokens. For example, when running on GCE the VM has access to a metadata server that can create these tokens for any application running on the VM. As another example, you can download a [service account keyfile](https://cloud.google.com/iam/docs/creating-managing-service-account-keys) and the C++ client libraries will create access tokens using the contents of this file.\n\nAccess tokens are bearer tokens. Having the token authenticates the principal, they can be used in separate connections, or from different computers than the one used to create them. If these tokens are exposed to third-parties the token can be used to make calls on behalf of the principal identified by the token. To reduce exposure, access tokens are always time limited. They automatically expire after a period of time (usually one hour).\n\nThe client libraries automatically refresh access tokens, that is, create new tokens before they expire. The only exception is `MakeAccessTokenCredentials()`, where the application provides the access token.\n\nThe client libraries also support authentication without a principal via API keys. Note that while the client libraries support sending the API key, not all Google Cloud Platform services support this method of authentication.\n\n### Development Workstations\n\nWe find that developers typically use Application Default Credentials to test their applications. The developer would either:\n\n1. Use `gcloud auth application-default` to authenticate using the developer's account. Keep in mind that this persists a \"refresh token\" in your workstation's filesystem. This refresh token is long-lived and can be used to create access tokens with full access to all GCP services.\n2. Set the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to load a service account key. The value of this environment variable is the full path of a file that contains the service account key. Keep in mind the security implications of keeping such a file as plain text in your filesystem.\n3. If you are using a GCE instance as your development environment, simply use the service account of the GCE machine to access GCP services.\n\n### Limitations\n\nThe C++ authentication components do not allow applications to create their own credential types. It is not possible to extend the C++ libraries without changing internal components. If you need additional functionality please file a [feature request](https://github.com/googleapis/google-cloud-cpp/issues) on GitHub. Likewise, creating the components that implement (as opposed to *describing* ) authentication flows are also considered implementation details. If you would like to use them in your own libraries please file a [feature request](https://github.com/googleapis/google-cloud-cpp/issues). We cannot promise that we will be able to satisfy these requests, but we will give them full consideration.\n\nImportant\\*\\*: If you accept a credential configuration (credential JSON/File/Stream) from an external source for authentication to Google Cloud Platform, you must validate it before providing it to any Google API or client library. Providing an unvalidated credential configuration to Google APIs can compromise the security of your systems and data. For more information, refer to [externally sourced credentials](https://cloud.google.com/docs/authentication/external/externally-sourced-credentials).\n\n### Classes\n\n- [`google::cloud::Credentials`](/cpp/docs/reference/common/latest/classgoogle_1_1cloud_1_1Credentials)\n- [`google::cloud::UnifiedCredentialsOption`](/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1UnifiedCredentialsOption)\n- [`google::cloud::DelegatesOption`](/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1DelegatesOption)\n- [`google::cloud::ScopesOption`](/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1ScopesOption)\n- [`google::cloud::AccessTokenLifetimeOption`](/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1AccessTokenLifetimeOption)\n- [`google::cloud::CARootsFilePathOption`](/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1CARootsFilePathOption)\n\n### Functions\n\n- [`MakeInsecureCredentials(Options)`](about:invalid#zCSafez)\n- [`MakeGoogleDefaultCredentials(Options)`](about:invalid#zCSafez)\n- [`MakeAccessTokenCredentials(std::string const &, std::chrono::system_clock::time_point, Options)`](about:invalid#zCSafez)\n- [`MakeImpersonateServiceAccountCredentials(std::shared_ptr\u003c Credentials \u003e, std::string, Options)`](about:invalid#zCSafez)\n- [`MakeServiceAccountCredentials(std::string, Options)`](about:invalid#zCSafez)\n- [`MakeExternalAccountCredentials(std::string, Options)`](about:invalid#zCSafez)\n- [`MakeApiKeyCredentials(std::string, Options)`](about:invalid#zCSafez)"]]