AI-generated Key Takeaways
-
This quickstart shows you how to obtain an OAuth token and use a Python script to send requests to the Data Portability API endpoints.
-
You will learn to use a Python script to send authenticated requests for initiating an archive, checking its state, downloading data, storing tokens, and revoking authorization.
-
Prerequisites include verifying API availability, completing setup steps, obtaining access to an account, and setting up OAuth, Python, and necessary Python packages.
-
The quickstart script utilizes a localhost redirect for the OAuth flow and can be run with various options for resources and time filtering.
In this quickstart, you obtain an OAuth token for your account, and you send requests to the Data Portability API endpoints using a Python script.
What you learn
In this quickstart you use a Python script to:
- Send an authenticated request to the
InitiatePortabilityArchiveendpoint by providing a valid OAuth token. The response is a validjob_id. - Send an authenticated request to the
GetPortabilityArchiveStateendpoint and download the data. The response should contain a valid job state, and when the job is complete, a signed URL. - Store OAuth token with time-based access for later reuse.
- Send an authenticated request to the
ResetAuthorizationendpoint. This request revokes all user-granted OAuth scopes. - Send an authenticated request to the
InitiatePortabilityArchiveendpoint by providing a valid OAuth token along withstart_timeorend_timeto apply a time filter to your request.
Prerequisites
To run this quickstart, you need to:
- Verify that the Data Portability API is available to you. For a list of supported countries and regions, see Common Questions on the "Share a copy of your data with a third party" page.
- Complete the setup steps for the Data Portability API, and verify that you're using a new project.
- Obtain access to an account owned or controlled by your organization. This account's search activity data is exported in this quickstart.
Before you begin this quickstart, you also need to set up OAuth, Python, and the OAuth package for the Python client libraries.
Set up OAuth
- Follow the steps to configure OAuth for
installed applications. This flow is similar to
the OAuth flow for web server applications. When you configure
your OAuth client ID:
- The Application type should be Desktop app.
- After you create the client ID, download the client secret in JSON
format, and name the file:
client_secrets.json. - When you configure scopes for the Data Portability API, note
that this quickstart uses these resource groups:
myactivity.search: https://www.googleapis.com/auth/dataportability.myactivity.searchmyactivity.youtube: https://www.googleapis.com/auth/dataportability.myactivity.youtube
Set up Python
You need to set up Python on your local machine to run the script used in this quickstart. To setup Python:
- Install the Pip package management tool and Python 3.11 (or higher).
- Install the Google API Client Library for Python.
Install the
google_auth_oauthlibpackage for the Python client library by running this command. This package provides oauthlib integration withgoogle-auth—the Google authentication library for Python.pip install google-auth-oauthlib google-api-python-client --upgrade
Run the script
This script uses a localhost redirect in the OAuth flow. This option requires the application to listen on a localhost port, and then open the system browser to initiate the OAuth authorization flow.
Click this link to open the script, or download a copy of the script by right-clicking the link.
Data Portability API Python quickstart script
To run the script and export the full data corpus, enter this command:
python3 data-portability-quickstart.pyTo use different scopes, specify them using --resources, for example:
python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.playTo apply time filtering, use the same script with start_time
and end_time flags. For example:
python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00ZThe actions taken by the script are detailed in the comments.