Getting Started with the Google Data Python Library

https://developers.google.com/gdata/articles/python_client_lib

Jeffrey Scudder, Google Data APIs Team, August 2007

Introduction

So you’ve decided to use the Google Data Python client library to write an application using one of the many Google Data services. Excellent choice! My aim with this short tutorial is to quickly get you started in using the client library to develop your application.

You probably want to jump in and start creating your application right away. First though, you may need to configure your development environment and set up the tools you’ll need to run the modules included in the client library. Follow the steps below and you’ll be running code in no time.

Installing Python on Windows

There are quite a few implementations of Python to choose from in Windows, but for purposes of this guide, I’ll be using the .msi installer found onpython.org.

  1. Begin by downloading the installer from the Python download page.
  2. Run the installer – you can accept all the default settings – [Install to c:/python33] -
  3. To see if your install is working as expected, open a command prompt – [press Windows Start + x together, select Command Prompt (Admin)] – and run python -V.

Installing Dependencies

Currently, the only external dependency is an XML library named ElementTree. If you are using Python version 2.5 or higher, you won’t need to install ElementTree since it comes with the Python package.

Installing the Google Data Library

Download the Google Data Python library if you haven’t done so. Look for the latest version [*.zip] on the Python project’s downloads page.

After downloading the library to c:/python33, unpack it using unzip to c:/python33/gdata-2.0.18.

Now you are ready to install the library modules so that they can be imported into Python. There are several ways you can do this:

  • If you have the ability to install packages for all users to access, open a command prompt, navigate to c:/python33/gdata-2.0.18 and run setup.py install.

Once you’ve installed the Google Data library, you’re ready to take the library for a test drive.

Running Tests and Samples

The Google Data Python client library distributions include some test cases which are used in the development of the library. They can also serve as a quick check to make sure that your dependencies and library installation are working. From the top level directory where you’ve unpacked your copy of the library, try running:

./tests/run_data_tests.py

If this script runs correctly, you should see output on the command line like this:

Running all tests in module gdata_test
.......
----------------------------------------------------------------------
Ran 7 tests in 0.025s

OK

Running all tests in module atom_test
..........................................
----------------------------------------------------------------------
Ran 42 tests in 0.016s

OK

...

If you did not see any errors as the tests execute, then you have probably set up your environment correctly. Congratulations!

Actually, get syntax error – apply this patch?

Reported by tobioatom, Aug 1, 2011
remove gdata_tests.base_test
so this test won't break.
run_data_tests.py.patch
894 bytes   View   Download
Oct 7, 2011

Project Member #1 afs…@google.com

Thanks for the patch, there were a few other odds and ends needed removing, but it is applied. Unfortunately a number of the core service tests depends on Google Base, which is now turned off.

Don't know effect of turning off Google Base!

Now you can start running something more interesting. The distribution contains a samples directory which contains code which can provide a starting point for writing your application. If you’d like to try out a simple interactive sample, try running ./samples/docs/docs_example.py.

Open command prompt, navigate to ./samples/webmastertools

The Google Documents List API sample will prompt you for the email address and password for your Google account. If you have any documents or spreadsheets in Google Documents, you can list them by entering 1 for your selected operation. (If you don’t have any documents or spreadsheets, you’ll get a 404 error.)

If there is a particular Google Data service which interestes you, take a look at the sample for it to get a basic idea of how you can use the library. Most, but not all, of the features are demonstrated in the sample code, so you might want to take a look at the documentation for the service you plan to work with to get a clear picture of what you can do with the API.

Writing a “Hello World” Example

Let’s start with a simple example. Here’s a short program to print a list of all of the documents in your Google Documents account:

import gdata.docs.service

# Create a client class which will make HTTP requests with Google Docs server.
client = gdata.docs.service.DocsService()
# Authenticate using your Google Docs email address and password.
client.ClientLogin('jo@gmail.com', 'password')

# Query the server for an Atom feed containing a list of your documents.
documents_feed = client.GetDocumentListFeed()
# Loop through the feed and extract each document entry.
for document_entry in documents_feed.entry:
  # Display the title of the document on the command line.
  print document_entry.title.text

Either save the above code snippet as a file and run it, or paste the code into the Python interpreter to see the Google Data Python client library at work.

Conclusion

Now that you’ve installed and tested the Google Data Python client library, you’re ready to start writing the next great application using:

As you continue to develop your application, you may hit a snag. If so, please check out the list of resources below:

If you happen to think of a great new feature for the library (or by chance find a bug), please enter it in the discussion group. We’re always interested in your feedback!

Happy coding :-)

 

The following two tabs change content below.

allenpg

Latest posts by allenpg (see all)

Leave a Reply