Some applications cannot use the default authentication mechanism (known as [Application Default Credentials]). You can override this default using google::cloud::UnifiedCredentialsOption. The following example shows how to explicitly load a service account key file.
namespace spanner = ::google::cloud::spanner;
[](std::string const& project_id, std::string const& instance_id,
std::string const& database_id, std::string const& keyfile) {
auto is = std::ifstream(keyfile);
is.exceptions(std::ios::badbit);
auto contents = std::string(std::istreambuf_iterator<char>(is.rdbuf()), {});
auto options =
google::cloud::Options{}.set<google::cloud::UnifiedCredentialsOption>(
google::cloud::MakeServiceAccountCredentials(contents));
return spanner::Client(spanner::MakeConnection(
spanner::Database(project_id, instance_id, database_id), options));
}
Keep in mind that we chose this as an example because it is relatively easy to understand. Consult the [Best practices for managing service account keys] guide for more details.
Follow these links to find examples for other *Client classes:
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-22 UTC."],[[["\u003cp\u003eThis page provides access to documentation for various versions of the Spanner C++ client library, ranging from version 2.11.0 to the latest release candidate 2.37.0-rc.\u003c/p\u003e\n"],["\u003cp\u003eThe content focuses on how to override the default authentication mechanism using \u003ccode\u003egoogle::cloud::UnifiedCredentialsOption\u003c/code\u003e for scenarios where Application Default Credentials are not suitable.\u003c/p\u003e\n"],["\u003cp\u003eThe provided example demonstrates how to explicitly load a service account key file for authentication with Spanner, emphasizing it as a simple illustration.\u003c/p\u003e\n"],["\u003cp\u003eAdditional examples for \u003ccode\u003eDatabaseAdminClient\u003c/code\u003e and \u003ccode\u003eInstanceAdminClient\u003c/code\u003e classes, regarding service account authentication, can be found using the provided links.\u003c/p\u003e\n"]]],[],null,["Version 2.35.0keyboard_arrow_down\n\n- [2.42.0-rc (latest)](/cpp/docs/reference/spanner/latest/spanner-auth-example)\n- [2.41.0](/cpp/docs/reference/spanner/2.41.0/spanner-auth-example)\n- [2.40.0](/cpp/docs/reference/spanner/2.40.0/spanner-auth-example)\n- [2.39.0](/cpp/docs/reference/spanner/2.39.0/spanner-auth-example)\n- [2.38.0](/cpp/docs/reference/spanner/2.38.0/spanner-auth-example)\n- [2.37.0](/cpp/docs/reference/spanner/2.37.0/spanner-auth-example)\n- [2.36.0](/cpp/docs/reference/spanner/2.36.0/spanner-auth-example)\n- [2.35.0](/cpp/docs/reference/spanner/2.35.0/spanner-auth-example)\n- [2.34.0](/cpp/docs/reference/spanner/2.34.0/spanner-auth-example)\n- [2.33.0](/cpp/docs/reference/spanner/2.33.0/spanner-auth-example)\n- [2.32.0](/cpp/docs/reference/spanner/2.32.0/spanner-auth-example)\n- [2.31.0](/cpp/docs/reference/spanner/2.31.0/spanner-auth-example)\n- [2.30.0](/cpp/docs/reference/spanner/2.30.0/spanner-auth-example)\n- [2.29.0](/cpp/docs/reference/spanner/2.29.0/spanner-auth-example)\n- [2.28.0](/cpp/docs/reference/spanner/2.28.0/spanner-auth-example)\n- [2.27.0](/cpp/docs/reference/spanner/2.27.0/spanner-auth-example)\n- [2.26.0](/cpp/docs/reference/spanner/2.26.0/spanner-auth-example)\n- [2.25.1](/cpp/docs/reference/spanner/2.25.1/spanner-auth-example)\n- [2.24.0](/cpp/docs/reference/spanner/2.24.0/spanner-auth-example)\n- [2.23.0](/cpp/docs/reference/spanner/2.23.0/spanner-auth-example)\n- [2.22.1](/cpp/docs/reference/spanner/2.22.1/spanner-auth-example)\n- [2.21.0](/cpp/docs/reference/spanner/2.21.0/spanner-auth-example)\n- [2.20.0](/cpp/docs/reference/spanner/2.20.0/spanner-auth-example)\n- [2.19.0](/cpp/docs/reference/spanner/2.19.0/spanner-auth-example)\n- [2.18.0](/cpp/docs/reference/spanner/2.18.0/spanner-auth-example)\n- [2.17.0](/cpp/docs/reference/spanner/2.17.0/spanner-auth-example)\n- [2.16.0](/cpp/docs/reference/spanner/2.16.0/spanner-auth-example)\n- [2.15.1](/cpp/docs/reference/spanner/2.15.1/spanner-auth-example)\n- [2.14.0](/cpp/docs/reference/spanner/2.14.0/spanner-auth-example)\n- [2.13.0](/cpp/docs/reference/spanner/2.13.0/spanner-auth-example)\n- [2.12.0](/cpp/docs/reference/spanner/2.12.0/spanner-auth-example)\n- [2.11.0](/cpp/docs/reference/spanner/2.11.0/spanner-auth-example) \n\nOverride the authentication configuration\n=========================================\n\nSome applications cannot use the default authentication mechanism (known as \\[Application Default Credentials\\]). You can override this default using [`google::cloud::UnifiedCredentialsOption`](https://cloud.google.com/cpp/docs/reference/common/latest/structgoogle_1_1cloud_1_1UnifiedCredentialsOption.html). The following example shows how to explicitly load a service account key file. \n\n namespace spanner = ::google::cloud::spanner;\n [](std::string const& project_id, std::string const& instance_id,\n std::string const& database_id, std::string const& keyfile) {\n auto is = std::ifstream(keyfile);\n is.exceptions(std::ios::badbit);\n auto contents = std::string(std::istreambuf_iterator\u003cchar\u003e(is.rdbuf()), {});\n auto options =\n google::cloud::Options{}.set\u003cgoogle::cloud::UnifiedCredentialsOption\u003e(\n google::cloud::MakeServiceAccountCredentials(contents));\n return spanner::Client(spanner::MakeConnection(\n spanner::Database(project_id, instance_id, database_id), options));\n }\n\nKeep in mind that we chose this as an example because it is relatively easy to understand. Consult the \\[Best practices for managing service account keys\\] guide for more details.\n\nFollow these links to find examples for other `*Client` classes:\n\n- [`DatabaseAdminClient`](/cpp/docs/reference/spanner/2.35.0/DatabaseAdminClient-with-service-account-snippet)\n- [`InstanceAdminClient`](/cpp/docs/reference/spanner/2.35.0/InstanceAdminClient-with-service-account-snippet)"]]