Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Python Example

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:

  1. Go to the Google Developers Console .
  2. Select a project to which the client ID will be associated.
  3. In the left sidebar, under APIs & AUTH , select Credentials .
  4. Click Create new Client ID .
  5. In the Create Client ID window, choose Installed application .
  6. Click Create Client ID .

    The resulting client ID will look like the following example:

    Public/Private Key Pair
  7. 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:

  1. Generate an authentication URL.

    1. If you are running the sample on a machine where you have access to a browser, use:

      $ python storage-sample.py
      
    2. Otherwise, use the noauth_local_webserver switch to generate an authentication URL.

      $ python storage-sample.py --noauth_local_webserver
      
  2. Open the authentication URL and click Accept .

  3. Copy the code and complete the authentication process.

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.