Rajiv
Rajiv

Reputation: 412

GCP Download/Get Service Account Key

This is how am trying to create a key under a service account and when am trying to fetch privateKeyData which contain is a base64 representation of credentials file via serviceAccountKey.getPrivateKeyData, the method is returning empty, not sure what am I doing wrong here

Can anyone throw some light here,

public void createServiceAccountKey(String serviceAccountName) 
      {
        ServiceAccountKey serviceAccountKey = null;
        try {
          initService();

          serviceAccountKey =
              service
                  .projects()
                  .serviceAccounts()
                  .keys()
                  .create(
                      "projects/-/serviceAccounts/"
                          + serviceAccountName
                          + "@"
                          + serviceAccountConfig.getProjectId()
                          + ".iam.gserviceaccount.com",
                      new CreateServiceAccountKeyRequest())
                  .execute();

          log.info("Created key under Service Account: {}", serviceAccountName);
          log.info("Service Account Private Data: ", serviceAccountKey.getPrivateKeyData());
          log.info("Service Account Key Type: ", serviceAccountKey.getKeyType());
          log.info("Service Account Key Type: ", serviceAccountKey.getKeyAlgorithm());

        } catch (IOException e) {
          log.error("Unable to create key under service account: {}", e.toString());
        }
      }

Upvotes: 0

Views: 497

Answers (1)

tanujabhoyar
tanujabhoyar

Reputation: 76

According to GCP documentation The format of the key may differ depending on how it is generated.

Because the formatting differs between each method, it's easiest to generate a key using the same method you plan to use when making future API calls. For example, if you're using gcloud, also generate your key using gcloud. To use a key for one method that's been generated using a different method (such as using a REST-generated key with gcloud), you'll need to edit the key to match the appropriate format

This might be helpful, look for how you are using and generating the keys.

Upvotes: 1

Related Questions