This library contains helpers to create OAuth2 access tokens. This may be useful when trying to access customer services deployed to Cloud Run, or when using Google APIs that lack a client library ingoogle-cloud-cpp.
Quickstart
The following shows the code that you'll run in thegoogle/cloud/oauth2/quickstart/directory. This is the typical usage of the library in applications.
#include "google/cloud/oauth2/access_token_generator.h"
#include <iostream>
int main(int argc, char* argv[]) try {
if (argc != 1) {
std::cerr << "Usage: " << argv[0] << "\n";
return 1;
}
// Use Application Default Credentials to initialize an AccessTokenGenerator
// retrieve the access token.
auto credentials = google::cloud::MakeGoogleDefaultCredentials();
auto generator =
google::cloud::oauth2::MakeAccessTokenGenerator(*credentials);
auto token = generator->GetToken();
if (!token) throw std::move(token).status();
// Only print the first few characters to avoid leaking tokens in test logs.
std::cout << "The access token starts with " << token->token.substr(0, 16)
<< "\n";
return 0;
} catch (google::cloud::Status const& status) {
std::cerr << "google::cloud::Status thrown: " << status << "\n";
return 1;
}
[[["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\u003eThis page details the OAuth2 Access Token Generation Library, which facilitates the creation of OAuth2 access tokens for accessing customer services on Cloud Run or Google APIs.\u003c/p\u003e\n"],["\u003cp\u003eThe latest release candidate version is 2.37.0-rc, and the page provides links to documentation for version 2.29.0 up through version 2.36.0 as well as several previous versions all the way back to 2.16.0.\u003c/p\u003e\n"],["\u003cp\u003eThe library's main functionality is demonstrated via a quickstart code example, where it displays how to initialize an \u003ccode\u003eAccessTokenGenerator\u003c/code\u003e with Application Default Credentials to retrieve and display an access token.\u003c/p\u003e\n"],["\u003cp\u003eKey interfaces include \u003ccode\u003eoauth2::AccessTokenGenerator\u003c/code\u003e and \u003ccode\u003eoauth2::MakeAccessTokenGenerator()\u003c/code\u003e, which are documented further within the provided documentation.\u003c/p\u003e\n"],["\u003cp\u003eAdditional resources are available for understanding error handling and authentication methods at Google, providing further context on library usage and credential management.\u003c/p\u003e\n"]]],[],null,["Version 2.29.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/oauth2/latest)\n- [2.41.0](/cpp/docs/reference/oauth2/2.41.0)\n- [2.40.0](/cpp/docs/reference/oauth2/2.40.0)\n- [2.39.0](/cpp/docs/reference/oauth2/2.39.0)\n- [2.38.0](/cpp/docs/reference/oauth2/2.38.0)\n- [2.37.0](/cpp/docs/reference/oauth2/2.37.0)\n- [2.36.0](/cpp/docs/reference/oauth2/2.36.0)\n- [2.35.0](/cpp/docs/reference/oauth2/2.35.0)\n- [2.34.0](/cpp/docs/reference/oauth2/2.34.0)\n- [2.33.0](/cpp/docs/reference/oauth2/2.33.0)\n- [2.32.0](/cpp/docs/reference/oauth2/2.32.0)\n- [2.31.0](/cpp/docs/reference/oauth2/2.31.0)\n- [2.30.0](/cpp/docs/reference/oauth2/2.30.0)\n- [2.29.0](/cpp/docs/reference/oauth2/2.29.0)\n- [2.28.0](/cpp/docs/reference/oauth2/2.28.0)\n- [2.27.0](/cpp/docs/reference/oauth2/2.27.0)\n- [2.26.0](/cpp/docs/reference/oauth2/2.26.0)\n- [2.25.1](/cpp/docs/reference/oauth2/2.25.1)\n- [2.24.0](/cpp/docs/reference/oauth2/2.24.0)\n- [2.23.0](/cpp/docs/reference/oauth2/2.23.0)\n- [2.22.1](/cpp/docs/reference/oauth2/2.22.1)\n- [2.21.0](/cpp/docs/reference/oauth2/2.21.0)\n- [2.20.0](/cpp/docs/reference/oauth2/2.20.0)\n- [2.19.0](/cpp/docs/reference/oauth2/2.19.0)\n- [2.18.0](/cpp/docs/reference/oauth2/2.18.0)\n- [2.17.0](/cpp/docs/reference/oauth2/2.17.0)\n- [2.16.0](/cpp/docs/reference/oauth2/2.16.0) \n\nOAuth2 Access Token Generation Library\n======================================\n\nThis library contains helpers to create OAuth2 access tokens. This may be useful when trying to access customer services deployed to Cloud Run, or when using Google APIs that lack a client library in `google-cloud-cpp`.\n\n### Quickstart\n\nThe following shows the code that you'll run in the `google/cloud/oauth2/quickstart/` directory. This is the typical usage of the library in applications. \n\n #include \"google/cloud/oauth2/access_token_generator.h\"\n #include \u003ciostream\u003e\n\n int main(int argc, char* argv[]) try {\n if (argc != 1) {\n std::cerr \u003c\u003c \"Usage: \" \u003c\u003c argv[0] \u003c\u003c \"\\n\";\n return 1;\n }\n\n // Use Application Default Credentials to initialize an AccessTokenGenerator\n // retrieve the access token.\n auto credentials = google::cloud::MakeGoogleDefaultCredentials();\n auto generator =\n google::cloud::oauth2::MakeAccessTokenGenerator(*credentials);\n auto token = generator-\u003eGetToken();\n if (!token) throw std::move(token).status();\n // Only print the first few characters to avoid leaking tokens in test logs.\n std::cout \u003c\u003c \"The access token starts with \" \u003c\u003c token-\u003etoken.substr(0, 16)\n \u003c\u003c \"\\n\";\n\n return 0;\n } catch (google::cloud::Status const& status) {\n std::cerr \u003c\u003c \"google::cloud::Status thrown: \" \u003c\u003c status \u003c\u003c \"\\n\";\n return 1;\n }\n\n### Main interfaces\n\n- [`oauth2::AccessTokenGenerator`](/cpp/docs/reference/oauth2/2.29.0/classgoogle_1_1cloud_1_1oauth2_1_1AccessTokenGenerator)\n- [`oauth2::MakeAccessTokenGenerator`()](/cpp/docs/reference/oauth2/2.29.0/namespacegoogle_1_1cloud_1_1oauth2)\n\n### More Information\n\n- [Error Handling](https://cloud.google.com/cpp/docs/reference/common/latest/common-error-handling.html) - describes how the library reports errors.\n- [Authentication methods at Google](https://cloud.google.com/docs/authentication) - has more information about authentication and the Application Default credentials"]]