เรียกใช้ Data Portability API โดยใช้สคริปต์ Python
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ในการเริ่มต้นใช้งานอย่างรวดเร็วนี้ คุณจะได้รับโทเค็น OAuth สําหรับบัญชี และส่งคําขอไปยังปลายทาง Data Portability API โดยใช้สคริปต์ Python
สิ่งที่คุณเรียนรู้
ในการเริ่มต้นใช้งานอย่างรวดเร็วนี้ คุณจะใช้สคริปต์ Python เพื่อดำเนินการต่อไปนี้
- ส่งคำขอที่ตรวจสอบสิทธิ์แล้วไปยังปลายทาง
InitiatePortabilityArchive
โดยระบุโทเค็น OAuth ที่ถูกต้อง การตอบกลับเป็น job_id
ที่ถูกต้อง
- ส่งคำขอที่ตรวจสอบสิทธิ์ไปยังปลายทาง
GetPortabilityArchiveState
และดาวน์โหลดข้อมูล การตอบกลับควรมีสถานะงานที่ถูกต้อง และ URL ที่เซ็นชื่อเมื่องานเสร็จสมบูรณ์
- จัดเก็บโทเค็น OAuth ที่มีสิทธิ์เข้าถึงตามเวลาเพื่อใช้ซ้ำในภายหลัง
- ส่งคำขอที่ตรวจสอบสิทธิ์แล้วไปยังปลายทาง
ResetAuthorization
คำขอนี้จะเพิกถอนขอบเขต OAuth ทั้งหมดที่ผู้ใช้ให้สิทธิ์
- ส่งคำขอที่ตรวจสอบสิทธิ์ไปยังปลายทาง
InitiatePortabilityArchive
โดยระบุโทเค็น OAuth ที่ถูกต้องพร้อมกับ start_time
หรือ end_time
เพื่อใช้ตัวกรองเวลากับคำขอ
ข้อกำหนดเบื้องต้น
หากต้องการเรียกใช้การเริ่มต้นใช้งานอย่างรวดเร็วนี้ คุณต้องดำเนินการดังนี้
- ยืนยันว่า Data Portability API พร้อมให้บริการแก่คุณ ดูรายชื่อประเทศและภูมิภาคที่รองรับได้ที่คำถามที่พบบ่อยในหน้า "แชร์สำเนาข้อมูลกับบุคคลที่สาม"
- ทําตามขั้นตอนการตั้งค่าสําหรับ Data Portability API ให้เสร็จสมบูรณ์ และยืนยันว่าคุณใช้โปรเจ็กต์ใหม่
- รับสิทธิ์เข้าถึงบัญชีที่องค์กรของคุณเป็นเจ้าของหรือควบคุม
ระบบจะส่งออกข้อมูลกิจกรรมการค้นหาของบัญชีนี้ในการเริ่มต้นใช้งานด่วนนี้
ก่อนเริ่มต้นการเริ่มต้นใช้งานอย่างรวดเร็วนี้ คุณต้องตั้งค่า OAuth, Python และแพ็กเกจ OAuth สําหรับไลบรารีของไคลเอ็นต์ Python ด้วย
ตั้งค่า OAuth
- ทำตามขั้นตอนเพื่อกำหนดค่า OAuth สำหรับแอปพลิเคชันที่ติดตั้ง ขั้นตอนนี้คล้ายกับขั้นตอน OAuth สำหรับแอปพลิเคชันเว็บเซิร์ฟเวอร์ สิ่งที่จะเกิดขึ้นเมื่อคุณกำหนดค่ารหัสไคลเอ็นต์ OAuth มีดังนี้
- ประเภทแอปพลิเคชันควรเป็นแอปบนเดสก์ท็อป
- หลังจากสร้างรหัสไคลเอ็นต์แล้ว ให้ดาวน์โหลดรหัสลับไคลเอ็นต์ในรูปแบบ JSON และตั้งชื่อไฟล์เป็น
client_secrets.json
- เมื่อกําหนดขอบเขตสําหรับ Data Portability API โปรดทราบว่าการเริ่มต้นใช้งานอย่างรวดเร็วนี้ใช้กลุ่มทรัพยากรต่อไปนี้
ตั้งค่า Python
คุณต้องตั้งค่า Python ในเครื่องเพื่อเรียกใช้สคริปต์ที่ใช้ในคู่มือเริ่มต้นใช้งานฉบับย่อนี้ วิธีตั้งค่า Python
เรียกใช้สคริปต์
สคริปต์นี้ใช้การเปลี่ยนเส้นทาง localhost ในขั้นตอน OAuth ตัวเลือกนี้กำหนดให้แอปพลิเคชันต้องฟังพอร์ต localhost แล้วเปิดเบราว์เซอร์ของระบบเพื่อเริ่มขั้นตอนการให้สิทธิ์ OAuth
คลิกลิงก์นี้เพื่อเปิดสคริปต์ หรือดาวน์โหลดสคริปต์สำเนาโดยคลิกขวาที่ลิงก์
สคริปต์เริ่มต้นใช้งาน Data Portability API ของ Python
หากต้องการเรียกใช้สคริปต์และส่งออกชุดข้อมูลทั้งหมด ให้ป้อนคําสั่งนี้
python3 data-portability-quickstart.py
หากต้องการใช้ขอบเขตอื่น ให้ระบุขอบเขตโดยใช้ --resources
เช่น
python3 data-portability-quickstart.py --resources myactivity.shopping myactivity.play
หากต้องการใช้การกรองเวลา ให้ใช้สคริปต์เดียวกันกับ Flag start_time
และ end_time
เช่น
python3 data-portability-quickstart.py --start_time 2023-01-01T12:00:00Z --end_time 2024-01-01T12:00:00Z
การดำเนินการของสคริปต์จะแสดงอยู่ในความคิดเห็นอย่างละเอียด
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-25 UTC
[null,null,["อัปเดตล่าสุด 2025-07-25 UTC"],[[["\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."]]