Notification texts go here Contact Us Buy Now!

How can I authenticate to GCP using a service account key that was uploaded (not generated)?

Authenticating to GCP with an Uploaded Service Account Key

How can I authenticate to GCP using a service account key that was uploaded (not generated)?

To authenticate to GCP using a service account key that was uploaded, you need to use the projects.serviceAccounts.keys.upload method.

Steps:

  1. Generate a new service account key. You can do this in the Google Cloud console or using the gcloud command-line tool. When you generate the key, select the "JSON" format.
  2. Upload the service account key to your GCP project. You can do this using the projects.serviceAccounts.keys.upload method.
  3. Configure your application to use the service account key. You can do this by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of the service account key file.

Example:

The following code sample shows you how to use the projects.serviceAccounts.keys.upload method to upload a service account key:

  // Imports the Google Cloud client libraries.
  import (
    "context"
    "fmt"
    "io"

    iam "google.golang.org/api/iam/v1"
  )

  // uploadServiceAccountKey uploads a service account key to a project.
  func uploadServiceAccountKey(w io.Writer, projectID, serviceAccountEmail, pathToKeyFile string) error {
    // projectID := "my-project-id"
    // serviceAccountEmail := "service-account@my-project.iam.gserviceaccount.com"
    // pathToKeyFile := "/path/to/key.json"

    ctx := context.Background()

    service, err := iam.NewService(ctx)
    if err != nil {
      return fmt.Errorf("iam.NewService: %v", err)
    }

    // Get the service account.
    serviceAccount, err := service.Projects.ServiceAccounts.Get(fmt.Sprintf("projects/%s/serviceAccounts/%s", projectID, serviceAccountEmail)).Do()
    if err != nil {
      return fmt.Errorf("Projects.ServiceAccounts.Get: %v", err)
    }

    // Create the service account key.
    key := &iam.ServiceAccountKey{
      PrivateKeyData: "YOUR_PRIVATE_KEY_DATA",
    }

    // Upload the service account key.
    key, err = service.Projects.ServiceAccounts.Keys.Upload(serviceAccount.Name, key).Do()
    if err != nil {
      return fmt.Errorf("Projects.ServiceAccounts.Keys.Upload: %v", err)
    }

    fmt.Fprintf(w, "Service account key uploaded: %v\n", key.Name)

    return nil
  }
  

Additional Resources:

* Creating and Managing Service Account Keys * Authenticating to Cloud Platform

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.