Python Code Samples

The code samples below have not been updated since 2011 and are in the process of
being rewritten to work properly with the latest specification of the Google Cloud Print API. In
the meantime, please use the content on this page solely as a rough reference and do not base the
implementation of your application on it.

The following are provided as examples on how to interact with the Google Cloud Print API using
Python, and will be of relevance both whether you are looking to receive print jobs and manage
printers, or whether you wish to submit print jobs to GCP. You can easily interact with the Cloud
Print API using HTTP requests. Python provides some modules to encode multipart data and to
interact with the HTTP protocol using urllib and urllib2. All of the following examples will
assume you've imported the needed Python modules.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

CRLF = '\r\n'
BOUNDARY = mimetools.choose_boundary()
# The following are used for authentication functions.
FOLLOWUP_HOST = 'www.google.com/cloudprint'
FOLLOWUP_URI = 'select%2Fgaiaauth'
GAIA_HOST = 'www.google.com'
LOGIN_URI = '/accounts/ServiceLoginAuth'
LOGIN_URL = 'https://www.google.com/accounts/ClientLogin'
SERVICE = 'cloudprint'
OAUTH = 'This_should_contain_oauth_string_for_your_username'
# The following are used for general backend access.
CLOUDPRINT_URL = 'https://www.google.com/cloudprint'
# CLIENT_NAME should be some string identifier for the client you are writing.
CLIENT_NAME = 'Cloud Print API Client'
# The following object is used in the sample code, but is not necessary.
logger = (code not included here, but it should be an initialized logger object with the handler and formatting or your choice)
# All of the calls to GetUrl assume you've run something like this:
tokens = GetAuthTokens(email, password)

Note: To obtain the OAUTH constant above, get an oauth client id
by following this link.

MultiPart Form Data

When sending many of the requests you'll need to encode the request into multiple parts. Here's
a sample method of encoding your data.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

When you are passing in file_type, you could use the mimetypes module to get the
file type by calling mimetypes.guess_type(filename). Fields will typically be some
key-value pairs that the server will need. In our case, we'll typically pass in things like
printerid, content identifiers, print job title, etc. Files may consist of things like a printer
capabilities file, or a raw graphic file.

HTTP Get/Post Method

We will need a method for sending HTTP requests to the Cloud Print Service. We also need to
retrieve authentication tokens from Google's account authentication service; consequently the HTTP
requests need mechanisms to include this authentication data in the cookie headers. The following
method depends on the Python module urllib2. Here is a sample method for these HTTP requests.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Cookie Extraction

The following method is used to extract a cookie from a set-cookie string.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Utility Functions

The following functions perform some general utility operations that we'll use in our other code.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Authentication

Google Cloud Print uses Google login credentials for authorization and authentication. The token
dictionary from GaiaLogin() will be combined with the token dictionary for
GetLoginCredentials() to form a complete token dictionary. The following methods can
be used to retrieve the needed authentication credentials for a specific user. These methods make
use of the ClientLogin API. Note however that we recommend that developers use OAuth 2.0. See
here for more information on Google's
various auth mechanisms, and our Developer Guide for details on using GCP
with OAuth.

Google Credentials

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Auth Token

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Note: While API calls can be made to work with several different
Google authentication schemes, we recommend that you use OAuth2. Reference
this page to see how to change the
authentication request above to use OAuth2.

Google Cloud Print Service Queries

The following functions will illustrate how to make some general queries against the Cloud Print
Service backend. In order to get data, these functions require authentication tokens acquired from
GetAuthTokens(). The following examples assume
tokens = GetAuthTokens(valid_email@address>, valid_password).

Printer Queries

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Here is function that will send a register request without credentials.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

The following method will use RegisterAnonPrinter() above to complete the entire
self registration process:

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Authenticated Registration

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

Printer Jobs

The following functions demonstrate how to interact with printer jobs.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.

There are various types of print jobs you could submit, ranging from raw graphic files, PDF files,
to a Google document or email thread. Here is a method that will handle submitting these types of
printer jobs.

This code has not been updated since 2011 and is in the process of being
rewritten to work properly with the latest specification of the Google Cloud Print API. In the
meantime, please use it solely as a rough reference and do not base the implementation of your
application on it.