This page discusses how you can use the Google APIs Client Library for Python to send requests to the Google Cloud Storage JSON API, using OAuth authentication.
Specifically, the sample shown here gets metadata about a bucket you specify and lists the objects in the bucket. You can use the general steps shown here to run your own code.
This document assumes that you have the latest version of Python installed, and you are familiar with Python and the Google Cloud Storage concepts and operations presented in the Getting Started: Using the Developers Console guide.
1. Set up your environment
You can set up what you need to run this sample by creating a quickstart application and downloading it. Or, you can install the required Python modules with the following pip commands:
$ sudo pip install --upgrade google-api-python-client
$ sudo pip install --upgrade httplib2
$ sudo pip install --upgrade argparse
Create a folder that will hold the example code.
2. Create a client secrets file
The example uses a client secrets file to authenticate with
the Google Cloud Storage JSON API. Download the client secrets (JSON) file, rename
it
client_secrets.json
, and put in the root of the folder you created.
To generate a client secrets file:
- Go to the Google Developers Console .
- Select a project to which the client ID will be associated.
- In the left sidebar, under APIs & AUTH , select Credentials .
- Click Create new Client ID .
- In the Create Client ID window, choose Installed application .
-
Click
Create Client ID
.
The resulting client ID will look like the following example:
- Click Download JSON .
3. Create your code file
In this step, you will create the Python code file,
storage-sample.py
, also
in the root of the folder you created. Copy
the code from below and make the following changes:
-
Set
_API_VERSION
to the latest version. -
Set
_BUCKET_NAME
to the name of a bucket in the same project as client ID you created.
storage-sample.py
4. Run the sample
Your directory structure at this point should look like this:
. └── client_secrets.json └── storage-sample.py
When you run the sample for the first time, you will be asked to authenticate.
To run the sample:
-
Generate an authentication URL.
-
If you are running the sample on a machine where you have access to a browser, use:
$ python storage-sample.py
-
Otherwise, use the
noauth_local_webserver
switch to generate an authentication URL.$ python storage-sample.py --noauth_local_webserver
-
-
Open the authentication URL and click Accept .
-
Copy the code and complete the authentication process.