/*! @page bigtable-samples-grpc-credentials Example: Use Cloud Bigtable C++ client with various gRPC credential classes # Use gRPC Credential Classes with Cloud Bigtable C++ client ## Before you begin ### Set the Environment Variable There are many ways to authenticate with Google Cloud Platform, here we use the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to setup authentication based on a key file: `export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"` where [PATH] is file path for JSON file that contains either user account or Service account key. Please refer [this link][info-google-authentication] for more details on Google Authentication. ## Use Access Token Credentials Acquire Access Token Use [gcloud][print-access-token] to acquire an Access Token The following code shows how to use access token to connect to an `InstanceAdmin` endpoint. @snippet bigtable_grpc_credentials.cc test access token ## Use Refresh Token Credentials Sample code to connect to InstanceAdmin endpoint ``` #include "google/cloud/bigtable/instance_admin.h" #include "google/cloud/bigtable/instance_admin_client.h" int main(int argc, char* argv[]) try { std::string const project_id = argv[1]; grpc::string refresh_token = R"""({ "client_id": "XXXXXX.apps.googleusercontent.com", "client_secret": "", "refresh_token": "", "type": "authorized_user" })"""; auto call_credentials = grpc::GoogleRefreshTokenCredentials(refresh_token); auto channel_credentials = grpc::SslCredentials(grpc::SslCredentialsOptions()); auto credentials = grpc::CompositeChannelCredentials(channel_credentials, call_credentials); auto instance_admin_client( google::cloud::bigtable::CreateDefaultInstanceAdminClient( project_id, google::cloud::bigtable::ClientOptions(credentials))); google::cloud::bigtable::InstanceAdmin instance_admin(instance_admin_client); auto instances = instance_admin.ListInstances(); return 0; } catch (std::exception const& ex) { std::cerr << "Standard C++ exception raised: " << ex.what() << std::endl; return 1; } ``` ## Use JWT Access Token Credentials The following code shows how to use a JWT access token to connect to an `InstanceAdmin` endpoint. @snippet bigtable_grpc_credentials.cc test jwt access token ## Use Google Compute Engine Credentials The following code shows how to use a GCE credentials to connect to an `InstanceAdmin` endpoint. @snippet bigtable_grpc_credentials.cc test gce credentials One may face "Request had insufficient authentication scopes." error while running above example. This might be due to disabled "Cloud API access scope" for Bigtable. This error can be removed by providing sufficient access as explained [here][api-access-scope]. ## Use of IAM Credentials These credentials are not supported yet. [info-google-authentication]: https://cloud.google.com/docs/authentication/getting-started [info-root-certificates]: https://github.com/GoogleCloudPlatform/google-cloud-cpp/tree/master/google/cloud/bigtable/examples#configure-grpc-root-certificates [print-access-token]: https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/print-access-token [api-access-scope]: https://cloud.google.com/compute/docs/access/create-enable-service-accounts-for-instances#changeserviceaccountandscopes */