CM0377 1999/2000 Lecture 1: Families program
Lab title: Google API Python Client
This lab explores the use of python APIs to access the SaaS facilities provided by the Google Cloud
Platform. Similar to the lab last week, we will use the Google Custom Search API to search a specific
website.
Task 0: Prerequisites
1) For this lab we will use Python 3.
2) Additionally, we will need the Python client library for Google’s discovery based APIs
(https://github.com/google/google-api-python-client). If you run the codes on the machines in the
Linux lab, then all prerequisites have been installed.
3) To run the codes from other machines, you need to install the prerequisites by yourself. For example,
on Ubuntu 16.04 LTS, you can install them using the command
sudo apt-get install python3 python3-googleapi
Task 1: Get an API Key
1) You need credentials to access APIs. To create a credential for the Google Custom Search API, go to
https://console.cloud.google.com/
and click “Go to APIs overview” to enter the API manager.
2) Click “Credentials” on the left panel, then click “Create credentials”, and select “API key”.
3) Copy the API key and save it to a safe place, then click “Close”.
Task 2: Get the Search Engine ID
1) You will also need a search engine ID to use the API for a specific custom search engine. Recall that
last week we created a custom search engine that searches within the COMSC website. Go to
https://cse.google.com/manage/all
and click on the name of that search engine.
2) In the “Details” section, click “Search engine ID”, copy the ID and save it at a safe place.
Task 3: Complete the Python Codes
1) Download the file main.py from the Learning Central.
2) In the following part of the file:
service = build(“customsearch”, “v1”,
developerKey=’
res = service.cse().list(
q=’
cx=’
).execute()
Replace
the search engine ID you got at Task 2. Replace
you want to use.
3) From the folder where you save the file main.py, run the following command in terminal
Page 1 of 2
https://github.com/google/google-api-python-client
https://cse.google.com/manage/all
https://console.cloud.google.com/
python3 main.py
This will return the custom search results in JSON format.
4) (Optional) For more options for the Custom Search API, check out https://google-api-client-
libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.html
Task 4: (Optional) Try Out Other APIs
1) You can find more sample codes for accessing other Google APIs from the following page:
https://github.com/google/google-api-python-client/tree/master/samples
2) You can try out different APIs by enabling the API’s from the API manager (see Task 1), and
modifying the sample codes.
3) Note that some APIs require additional credentials such as an OAuth client ID, which you can create
from the “Credentials” button of the API manager.
4) Also be aware that for some services you need to pay for the usage.
Page 2 of 2
https://google-api-client-libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.html
https://google-api-client-libraries.appspot.com/documentation/customsearch/v1/python/latest/customsearch_v1.cse.html
https://github.com/google/google-api-python-client/tree/master/samples
Lab title: Google API Python Client
Task 0: Prerequisites
Task 1: Get an API Key
Task 2: Get the Search Engine ID
Task 3: Complete the Python Codes
Task 4: (Optional) Try Out Other APIs