How to Override the Authentication Credentials

Unless otherwise configured, the client libraries useApplication Default Credentials to authenticate with Google Cloud Services. While this works for most applications, in some cases you may need to override this default. You can do so by providing theUnifiedCredentialsOption The following example shows how to explicitly load a service account key file:

  [](std::string const& keyfile) {    auto is = std::ifstream(keyfile);    is.exceptions(std::ios::badbit);  // Minimal error handling in examples    auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});    auto options =        google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(            google::cloud::MakeServiceAccountCredentials(contents));    return google::cloud::composer_v1::EnvironmentsClient(        google::cloud::composer_v1::MakeEnvironmentsConnection(options));  }

Follow these links to find examples for other*Client classes:

Keep in mind that we chose this as an example because it is relatively easy to understand. Consult theBest practices for managing service account keys guide for more details.

See Also

Authentication Components - for more information on the factory functions to creategoogle::cloud::Credentials objects.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.