# Example: Use Cloud Bigtable C++ client with various gRPC credential classes

3.7.0-rc 3.6.0 (latest) 3.5.0 3.4.0 3.3.0 3.2.0 2.48.0-rc 2.47.1 2.46.0 2.45.0 2.44.0 2.43.0 2.42.0 2.41.0 2.40.0 2.39.0 2.38.0 2.37.0 2.36.0 2.35.0 2.34.0 2.33.0 2.32.0 2.31.0 2.30.0 2.29.0 2.28.0 2.27.0 2.26.0 2.25.1 2.24.0 2.23.0 2.22.1 2.21.0 2.20.0 2.19.0 2.18.0 2.17.0 2.16.0 2.15.1 2.14.0 2.13.0 2.12.0 2.11.0

## 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](https://cloud.google.com/docs/authentication/client-libraries) for more details on Google Authentication.

### Use Access Token Credentials

Acquire Access Token

Use [gcloud](https://cloud.google.com/sdk/gcloud/reference/beta/auth/application-default/print-access-token) to acquire an Access Token

The following code shows how to use access token to connect to an Admin API endpoint.

      [](std::string const& project_id, std::string const& instance_id,
         std::string const& access_token) {
        auto creds = gc::MakeAccessTokenCredentials(
            access_token, std::chrono::system_clock::now() + std::chrono::hours(1));
        auto options = gc::Options{}.set<gc::UnifiedCredentialsOption>(creds);

        cbta::BigtableTableAdminClient admin(
            cbta::MakeBigtableTableAdminConnection(options));

        google::bigtable::admin::v2::ListTablesRequest r;
        r.set_parent(cbt::InstanceName(project_id, instance_id));
        r.set_view(google::bigtable::admin::v2::Table::NAME_ONLY);
        auto tables = admin.ListTables(std::move(r));
        for (auto& table : tables) {
          if (!table) throw std::move(table).status();
        }
      }

### Use Refresh Token Credentials

Sample code to connect to an Admin API endpoint

    #include "google/cloud/bigtable/admin/bigtable_instance_admin_client.h"
    #include "google/cloud/project.h"

    int main(int argc, char* argv[]) try {
      namespace cbta = ::google::cloud::bigtable_admin;
      namespace gc = ::google::cloud;
      std::string const project_id = argv[1];
      grpc::string refresh_token = R"""({
          "client_id": "XXXXXX.apps.googleusercontent.com",
          "client_secret": "<actual secret here>",
          "refresh_token": "<actual token value here>",
          "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 client = cbta::BigtableInstanceAdminClient(
          cbta::MakeBigtableInstanceAdminConnection(
              gc::Options{}.set<gc::GrpcCredentialOption>(credentials)));
      auto instances = client.ListInstances(gc::Project(project_id).FullName()));
      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 Admin API endpoint.

      [](std::string const& project_id, std::string const& instance_id,
         std::string const& service_account_file_json) {
        std::ifstream stream(service_account_file_json);
        if (!stream.is_open()) {
          std::ostringstream os;
          os << "JWTAccessToken(" << service_account_file_json
             << "): cannot open upload file source";
          throw std::runtime_error(os.str());
        }
        std::string json_key(std::istreambuf_iterator<char>{stream}, {});

        auto creds = gc::MakeServiceAccountCredentials(std::move(json_key));
        auto options = gc::Options{}.set<gc::UnifiedCredentialsOption>(creds);

        cbta::BigtableTableAdminClient admin(
            cbta::MakeBigtableTableAdminConnection(options));

        google::bigtable::admin::v2::ListTablesRequest r;
        r.set_parent(cbt::InstanceName(project_id, instance_id));
        r.set_view(google::bigtable::admin::v2::Table::NAME_ONLY);
        auto tables = admin.ListTables(std::move(r));
        for (auto& table : tables) {
          if (!table) throw std::move(table).status();
        }
      }

### Use Google Compute Engine Credentials

The following code shows how to use a GCE credentials to connect to an Admin API endpoint.

### Use of IAM Credentials

#### Check IAM Policy

      namespace cbt = ::google::cloud::bigtable;
      namespace cbta = ::google::cloud::bigtable_admin;
      using ::google::cloud::StatusOr;
      [](cbta::BigtableTableAdminClient admin, std::string const& project_id,
         std::string const& instance_id, std::string const& table_id) {
        std::string table_name = cbt::TableName(project_id, instance_id, table_id);
        StatusOr<google::iam::v1::Policy> policy = admin.GetIamPolicy(table_name);
        if (!policy) throw std::move(policy).status();
        std::cout << "The IAM Policy for " << table_id << " is\n"
                  << policy->DebugString() << "\n";
      }

#### Set IAM Policy

      namespace cbt = ::google::cloud::bigtable;
      namespace cbta = ::google::cloud::bigtable_admin;
      using ::google::cloud::StatusOr;
      [](cbta::BigtableTableAdminClient admin, std::string const& project_id,
         std::string const& instance_id, std::string const& table_id,
         std::string const& role, std::string const& member) {
        std::string table_name = cbt::TableName(project_id, instance_id, table_id);
        StatusOr<google::iam::v1::Policy> current = admin.GetIamPolicy(table_name);
        if (!current) throw std::move(current).status();
        // This example adds the member to all existing bindings for that role. If
        // there are no such bindings, it adds a new one. This might not be what the
        // user wants, e.g. in case of conditional bindings.
        size_t num_added = 0;
        for (auto& binding : *current->mutable_bindings()) {
          if (binding.role() == role) {
            binding.add_members(member);
            ++num_added;
          }
        }
        if (num_added == 0) {
          *current->add_bindings() = cbt::IamBinding(role, {member});
        }
        StatusOr<google::iam::v1::Policy> policy =
            admin.SetIamPolicy(table_name, *current);
        if (!policy) throw std::move(policy).status();
        std::cout << "The IAM Policy for " << table_id << " is\n"
                  << policy->DebugString() << "\n";
      }

#### Check IAM Permissions

      using ::google::cloud::StatusOr;
      namespace cbt = ::google::cloud::bigtable;
      namespace cbta = ::google::cloud::bigtable_admin;

      [](cbta::BigtableTableAdminClient admin, std::string const& project_id,
         std::string const& instance_id, std::string const& table_id,
         std::vector<std::string> const& permissions) {
        std::string table_name = cbt::TableName(project_id, instance_id, table_id);
        auto result = admin.TestIamPermissions(table_name, permissions);
        if (!result) throw std::move(result).status();
        std::cout << "The current user has the following permissions [";
        std::cout << absl::StrJoin(result->permissions(), ", ");
        std::cout << "]\n";
      }