使用 Python 指令碼呼叫 Data Portability API
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
在本快速入門課程中,您將為帳戶取得 OAuth 權杖,並使用 Python 指令碼將要求傳送至 Data Portability API 端點。
課程內容
在本快速入門導覽課程中,您將使用 Python 指令碼執行以下操作:
- 提供有效的 OAuth 權杖,將經過驗證的要求傳送至
InitiatePortabilityArchive
端點。回應為有效的 job_id
。
- 將已驗證的要求傳送至
GetPortabilityArchiveState
端點,然後下載資料。回應應包含有效的工作狀態,並在工作完成時提供已簽署的網址。
- 儲存 OAuth 權杖,並設定時間限制,以便日後重複使用。
- 將已驗證的要求傳送至
ResetAuthorization
端點。這項要求會撤銷所有使用者授予的 OAuth 範圍。
- 提供有效的 OAuth 權杖,並搭配
start_time
或 end_time
,將時間篩選器套用至要求,藉此向 InitiatePortabilityArchive
端點傳送經過驗證的要求。
必要條件
如要執行這個快速入門,您需要:
- 確認您可以使用資料可攜權 API。如需支援國家/地區清單,請參閱「與第三方分享資料副本」頁面中的常見問題。
- 完成 Data Portability API 的設定步驟,並確認您使用的是新專案。
- 取得貴機構擁有或控管的帳戶存取權。這個帳戶的搜尋活動資料會在本快速入門課程中匯出。
開始本快速入門課程前,您還需要為 Python 用戶端程式庫設定 OAuth、Python 和 OAuth 套件。
設定 OAuth
- 請按照已安裝應用程式的設定步驟設定 OAuth。這個流程與網路伺服器應用程式的 OAuth 流程類似。設定 OAuth 用戶端 ID 時:
- 應用程式類型應為「電腦版應用程式」。
- 建立用戶端 ID 後,請以 JSON 格式下載用戶端密鑰,並將檔案命名為
client_secrets.json
。
- 為 Data Portability API 設定範圍時,請注意,本快速入門會使用以下資源群組:
設定 Python
您必須在本機電腦上設定 Python,才能執行本快速入門課程中使用的指令碼。如要設定 Python,請按照下列步驟操作:
執行指令碼
這個指令碼會在 OAuth 流程中使用本機重新導向。這個選項需要應用程式在本機主機通訊埠上監聽,然後開啟系統瀏覽器來啟動 OAuth 授權流程。
按一下這個連結即可開啟指令碼,或按一下連結右鍵下載指令碼的副本。
資料可攜權 API Python 快速入門指令碼
如要執行指令碼並匯出完整資料集,請輸入下列指令:
python3 data-portability-quickstart.py
如要使用不同的範圍,請使用 --resources
指定範圍,例如:
python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.play
如要套用時間篩選功能,請使用含有 start_time
和 end_time
旗標的相同指令碼。例如:
python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00Z
註解中會詳細說明指令碼執行的動作。
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[[["\u003cp\u003eThis quickstart guides you through using a Python script to interact with the Google Data Portability API for exporting user data.\u003c/p\u003e\n"],["\u003cp\u003eYou will learn to authenticate requests, initiate data archive creation, check the archive status, download data, and reset authorization.\u003c/p\u003e\n"],["\u003cp\u003eBefore starting, ensure the API is available in your region, complete setup steps including OAuth configuration for a desktop application, and install the necessary Python packages.\u003c/p\u003e\n"],["\u003cp\u003eThe provided Python script uses a localhost redirect for the OAuth flow, and detailed instructions are given for running it.\u003c/p\u003e\n"]]],[],null,["# Call the Data Portability API Using a Python Script\n\nIn this quickstart, you obtain an OAuth token for your account, and you send\nrequests to the Data Portability API endpoints using a Python script.\n\nWhat you learn\n--------------\n\nIn this quickstart you use a Python script to:\n\n- Send an authenticated request to the `InitiatePortabilityArchive` endpoint by providing a valid OAuth token. The response is a valid `job_id`.\n- Send an authenticated request to the `GetPortabilityArchiveState` endpoint and download the data. The response should contain a valid job state, and when the job is complete, a signed URL.\n- Store OAuth token with time-based access for later reuse.\n- Send an authenticated request to the `ResetAuthorization` endpoint. This request revokes all user-granted OAuth scopes.\n- Send an authenticated request to the `InitiatePortabilityArchive` endpoint by providing a valid OAuth token along with `start_time` or `end_time` to apply a time filter to your request.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you need to:\n\n- Verify that the Data Portability API is available to you. For a list of supported countries and regions, see [Common Questions](https://support.google.com/accounts/answer/14452558) on the \"Share a copy of your data with a third party\" page.\n- Complete the [setup steps](/data-portability/user-guide/setup) for the Data Portability API, and verify that you're using a new project.\n- Obtain access to an account owned or controlled by your organization. This account's search activity data is exported in this quickstart.\n\nBefore you begin this quickstart, you also need to set up OAuth, Python, and the\nOAuth package for the Python client libraries.\n\n### Set up OAuth\n\n- Follow the steps to configure OAuth for [installed applications](https://googleapis.github.io/google-api-python-client/docs/oauth-installed.html). This flow is similar to the OAuth flow for [web server applications](/identity/protocols/oauth2/web-server). When you configure your OAuth client ID:\n - The **Application type** should be **Desktop app**.\n - After you create the client ID, download the client secret in JSON format, and name the file: `client_secrets.json`.\n - When you [configure scopes](/data-portability/user-guide/configure-oauth) for the Data Portability API, note that this quickstart uses these resource groups:\n - [`myactivity.search`](/data-portability/schema-reference/my_activity): https://www.googleapis.com/auth/dataportability.myactivity.search\n - [`myactivity.youtube`](/data-portability/schema-reference/my_activity): https://www.googleapis.com/auth/dataportability.myactivity.youtube\n\n### Set up Python\n\nYou need to set up Python on your local machine to run the script used in this\nquickstart. To setup Python:\n\n- Install the [Pip](https://pypi.org/project/pip/) package management tool and [Python 3.11](https://www.python.org/downloads/) (or higher).\n- Install the [Google API Client Library for Python](https://github.com/googleapis/google-api-python-client/blob/main/docs/start.md).\n- Install the `google_auth_oauthlib` package for the Python client library by\n running this command. This package provides [oauthlib](https://oauthlib.readthedocs.io/) integration\n with `google-auth`---the Google authentication library for Python.\n\n pip install google-auth-oauthlib google-api-python-client --upgrade\n\nRun the script\n--------------\n\nThis script uses a localhost redirect in the OAuth flow. This option requires\nthe application to listen on a localhost port, and then open the system browser\nto initiate the OAuth authorization flow.\n\nClick this link to open the script, or download a copy of the script by\nright-clicking the link.\n\n[Data Portability API Python quickstart script](/static/data-portability/user-guide/data-portability-quickstart.py)\n\nTo run the script and export the full data corpus, enter this command: \n\n python3 data-portability-quickstart.py\n\nTo use different scopes, specify them using `--resources`, for example: \n\n python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.play\n\nTo apply [time filtering](/data-portability/user-guide/time-filter), use the same script with `start_time`\nand `end_time` flags. For example: \n\n python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00Z\n\nThe actions taken by the script are detailed in the comments."]]