If you’re running in Compute Engine or App Engine, authentication should “just work”.
If you’re developing locally, the easiest way to authenticate is using the Google Cloud SDK:
$ gcloud beta auth application-default login
Note that this command generates credentials for client libraries. To authenticate the CLI itself, use:
$ gcloud auth login
gcloud auth loginwas used for both use cases. If your
gcloudinstallation does not support the new command, please update it:
$ gcloud components update
If you’re running your application elsewhere, you should download a service account JSON keyfile and point to it using an environment variable:
$ export GOOGLE_APPLICATION_CREDENTIALS="/path/to/keyfile.json"
Every package uses a
as a base for interacting with an API.
from google.cloud import datastore client = datastore.Client()
Passing no arguments at all will “just work” if you’ve followed the instructions in the Overview. The credentials are inferred from your local environment by using Google Application Default Credentials.
The Application Default Credentials discussed above can be useful if your code needs to run in many different environments or if you just don’t want authentication to be a focus in your code.
However, you may want to be explicit because
- your code will only run in one place
- you may have code which needs to be run as a specific service account every time (rather than with the locally inferred credentials)
- you may want to use two separate accounts to simultaneously access data from different projects
client = Client(credentials=credentials)
Google App Engine Environment#
just for Google App Engine:
from google.auth import app_engine credentials = app_engine.Credentials()
Google Compute Engine Environment#
just for Google Compute Engine:
from google.auth import compute_engine credentials = compute_engine.Credentials()
A service account is stored in a JSON keyfile.
For example, with a JSON keyfile:
client = Client.from_service_account_json('/path/to/keyfile.json')
Previously the Google Cloud Console would issue a PKCS12/P12 key for your service account. This library does not support that key format. You can generate a new JSON key for the same service account from the console.
User Accounts (3-legged OAuth 2.0) with a refresh token#
The majority of cases are intended to authenticate machines or workers rather than actual user accounts. However, it’s also possible to call Google Cloud APIs with a user account via OAuth 2.0.
A production application should use a service account,
but you may wish to use your own personal user account when first
getting started with the
The simplest way to use credentials from a user account is via
Application Default Credentials using
gcloud auth login
(as mentioned above) and
import google.auth credentials, project = google.auth.default()
This will still follow the precedence described above, so be sure none of the other possible environments conflict with your user provided credentials.
Advanced users of oauth2client can also use custom flows to
create credentials using client secrets or using a
can be serialized with
and stored in a file and then and deserialized with
from_json(). In order
oauth2client‘s credentials with this library, you’ll need to
Setting up a Service Account#
If your application is not running on Google Compute Engine, you need a Google Developers Service Account.
Visit the Google Developers Console.
Create a new project or click on an existing project.
Navigate to APIs & auth > APIs and enable the APIs that your application requires.
You may need to enable billing in order to use these services.
- BigQuery API
- Google Cloud Datastore API
- Google Cloud Pub/Sub
- Google Cloud Storage
- Google Cloud Storage JSON API
Navigate to APIs & auth > Credentials.
You should see a screen like one of the following:
Find the “Add credentials” drop down and select “Service account” to be guided through downloading a new JSON keyfile.
If you want to re-use an existing service account, you can easily generate a new keyfile. Just select the account you wish to re-use, and click Generate new JSON key:
Using Google Compute Engine#
If your code is running on Google Compute Engine, using the inferred Google Application Default Credentials will be sufficient for retrieving credentials.
However, by default your credentials may not grant you access to the services you intend to use. Be sure when you set up the GCE instance, you add the correct scopes for the APIs you want to access:
google-cloud-python library defaults to using google-auth
to sign requests and
httplib2 for sending requests,
it is not a strict requirement.
Client constructor accepts an optional
http argument in place of a
If passed, all HTTP requests made by the client will use your
custom HTTP object.
In order for this to be possible,
http object must do two things:
- Handle authentication on its own
- Define a method
request()that can subsitute for
The entire signature from
httplib2 need not be implemented,
we only use it as
http.request(uri, method=method_name, body=body, headers=headers)
We hope to enable using custom HTTP libraries with this library at some point.