• Digital accessories
  • Server
  • Digitalni život
  • Privacy policy
  • Contact us
  1. Home
  2. Server
  3. Getting started with authentication | Authentication | Google Cloud

Getting started with authentication | Authentication | Google Cloud

Rsdaa 15/01/2022 1719

This topic shows how to quickly authenticate to a Google Cloud API for testing purposes. It does not show how to authenticate to non-Google Cloud APIs, such as the Google Maps Platform APIs. For information about API keys for Google Maps Platform APIs and SDKs, see the Google Maps documentation.

Before proceeding, we recommend that all Google Cloud developers first read the Authentication overview topic to understand how authentication works in Google Cloud, including common scenarios and strategies. Additionally, before deploying an application to a production environment, ensure that you've read Authenticating as a service account.

Note: If your application runs on-premises, or on another cloud provider, you can use workload identity federation to grant access to external identities without using a service account key. Some client libraries can use Application Default Credentials (ADC) to automatically generate credentials for external identities.

To learn more, see the documentation for your identity provider:

Creating a service account

Create a service account:

In the Cloud Console, go to the Create service account page.

Go to Create service accountSelect a project.

In the Service account name field, enter a name. The Cloud Console fills in the Service account ID field based on this name.

In the Service account description field, enter a description. For example, Service account for quickstart.

Click Create and continue.

Click the Select a role field.

Under Quick access, click Basic, then click Owner.

Note: The Role field affects which resources your service account can access in your project. You can revoke these roles or grant additional roles later. In production environments, do not grant the Owner, Editor, or Viewer roles. For more information, seeGranting, changing, and revoking access to resources.Click Continue.

Click Done to finish creating the service account.

Do not close your browser window. You will use it in the next step.

Create a service account key:

In the Cloud Console, click the email address for the service account that you created.Click Keys.Click Add key, then click Create new key.Click Create. A JSON key file is downloaded to your computer.Click Close.

Create the service account. Replace NAME with a name for the service account.

gcloud iam service-accounts createNAME

Grant permissions to the service account. Replace PROJECT_ID with your project ID.

gcloud projects add-iam-policy-bindingPROJECT_ID --member="serviceAccount:NAME@PROJECT_ID.iam.gserviceaccount.com" --role="roles/owner"Note: The --role flag affects which resources your service account can access in your project. You can revoke these roles or grant additional roles later. In production environments, do not grant the Owner, Editor, or Viewer roles. For more information, seeGranting, changing, and revoking access to resources.

Generate the key file. Replace FILE_NAME with a name for the key file.

gcloud iam service-accounts keys createFILE_NAME.json --iam-account=NAME@PROJECT_ID.iam.gserviceaccount.com

Setting the environment variable

To use service accounts with the Cloud SDK, you need to set an environment variable where your code runs.

Provide authentication credentials to your application code by setting the environment variable GOOGLE_APPLICATION_CREDENTIALS. This variable only applies to your current shell session, so if you open a new session, set the variable again.

export GOOGLE_APPLICATION_CREDENTIALS=""

For example:

export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

For PowerShell:

$env:GOOGLE_APPLICATION_CREDENTIALS=""

For example:

$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

For command prompt:

set GOOGLE_APPLICATION_CREDENTIALS=

Setting the environment variable allows you to provide credentials separately from your application, without making changes to application code when you deploy. Alternately, you can explicitly specify the path to the service account key file in your code. For more information, see Authenticating as a service account.

Verifying authentication

After setting the environment variable, you don't need to explicitly specify your credentials in code when using a Google Cloud client library. The client library can determine your credentials implicitly. For this reason, you can verify that authentication works by setting the environment variable, and then running client library code, such as the following example. If the request succeeds, authentication works.

You must Note:install the Cloud Storage client library to run the following example.

What's next

Try it for yourself

If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.

Get started for free


PREV: A Guide to VPS vs Cloud Hosting: Understanding Five Key ...

NEXT: Hosting your own web server at home - The Silicon Underground

Popular Articles

Hot Articles

Navigation Lists

Back to Top