Python quickstart for resellers

Follow the steps in this quickstart guide, and in about 10 minutes you'll have a simple Python command-line app that makes requests to the zero-touch enrollment reseller API.

Prerequisites

To run this quickstart, you'll need:

  • A Google account, that's a member of your zero-touch enrollment reseller account. If you haven't onboarded yet, follow the steps in Get started in the Reseller portal guide.
  • Python 2.6 or greater.
  • The pip package management tool.
  • Access to the internet and a web browser.

Step 1: Turn on the zero-touch enrollment API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials .
  2. Set What data will you be accessing? to Application data.
  3. Click Next. You should be prompted to create a service account.
  4. Give a descriptive name for Service account name.
  5. Note the Service account ID (it looks like an email address) because you'll use it later.
  6. Set Role to Service Accounts > Service Account User.
  7. Click Done to finish creating the service account.
  8. Click the email address for the service account that you created.
  9. Click **Keys**.
  10. Click **Add key**, then click **Create new key**.
  11. For **Key type**, select **JSON**.
  12. Click Create and the private key downloads to your computer.
  13. Click **Close**.
  14. Move the file to your working directory and rename it service_account_key.json.
  1. Open the zero-touch enrollment portal. You might need to sign in.
  2. Click Service accounts.
  3. Click Link service account.
  4. Set Email address to the address of the service account you created.
  5. Click Link service account to use the service account with your zero-touch enrollment account.

Step 3: Install the Google client library

Run the following command to install the library using pip:

pip install --upgrade google-api-python-client

See the library's installation page for different installation options.

Step 4: Set up the sample

Create a file named quickstart.py in your working directory. Copy in the following code and save the file. Insert your own reseller partner ID as the value for PARTNER_ID (the app's first line after the imports).

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Zero-touch enrollment reseller quickstart.

This script forms the quickstart introduction to the zero-touch enrollemnt
reseller API. To learn more, visit https://developer.google.com/zero-touch
"""

from apiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

# TODO: replace this with your partner reseller ID.
PARTNER_ID = '11036885';

# A single auth scope is used for the zero-touch enrollment customer API.
SCOPES = ['https://www.googleapis.com/auth/androidworkprovisioning']
SERVICE_ACCOUNT_KEY_FILE = 'service_account_key.json'

def get_credential():
  """Creates a Credential object with the correct OAuth2 authorization.

  Creates a Credential object with the correct OAuth2 authorization
  for the service account that calls the reseller API. The service
  endpoint calls this method when setting up a new service instance.

  Returns:
    Credential, the user's credential.
  """
  credential = ServiceAccountCredentials.from_json_keyfile_name(
      SERVICE_ACCOUNT_KEY_FILE, scopes=SCOPES)
  return credential


def get_service():
  """Creates a service endpoint for the zero-touch enrollment reseller API.

  Builds and returns an authorized API client service for v1 of the API. Use
  the service endpoint to call the API methods.

  Returns:
    A service Resource object with methods for interacting with the service.
  """
  http_auth = get_credential().authorize(Http())
  service = build('androiddeviceprovisioning', 'v1', http=http_auth)
  return service


def main():
  """Runs the zero-touch enrollment quickstart app.
  """
  # Create a zero-touch enrollment API service endpoint.
  service = get_service()

  # Send an API request to list all our customers.
  response = service.partners().customers().list(partnerId=PARTNER_ID).execute()

  # Print out the details of each customer.
  if 'customers' in response:
    for customer in response['customers']:
      print 'Name:{0}  ID:{1}'.format(
          customer['companyName'], customer['companyId'])
  else:
    print 'No customers found'


if __name__ == '__main__':
  main()

Partner ID

API calls typically need your reseller partner ID as an argument. To find your partner ID from the zero-touch enrollment portal, follow the steps below:

  1. Open the portal. You might need to sign in.
  2. Click Service accounts.
  3. Copy your partner ID number from the Your reseller ID line.

Step 5: Run the sample

Use your operating system's help to run the script in the file. On UNIX and Mac computers, run the command below in your terminal:

python quickstart.py

Printing API responses

To make it easier to inspect responses when trying out the API, format the JSON response data. The snippet below shows how you might do this in Python using the JSON module:

from json import dumps

# ...

results = provisioning.partners().devices().claimAsync(partnerId=MY_PARTNER_ID,
 body={'claims':new_claims}).execute()
# Print formatted JSON response
print dumps(results, indent=4, sort_keys=True)

Troubleshooting

Tell us what went wrong with the quickstart and we'll work to fix it. To learn how zero-touch uses service accounts to authorize API calls, read Authorization.

Learn more