استدعاء واجهة برمجة تطبيقات إمكانية نقل البيانات باستخدام نص برمجي في بايثون
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
في هذا الدليل المخصّص للبدء السريع، يمكنك الحصول على رمز مميز لبروتوكول 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. يتطلّب هذا الخيار
أن يستمع التطبيق إلى منفذ مضيف محلي، ثم يفتح متصفّح النظام
لبدء عملية التفويض باستخدام بروتوكول OAuth.
انقر على هذا الرابط لفتح النص البرمجي، أو نزِّل نسخة من النص البرمجي من خلال
النقر بزر الماوس الأيمن على الرابط.
نص برمجي لبدء استخدام Data Portability 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
يتم تفصيل الإجراءات التي يتّخذها النص البرمجي في التعليقات.
إنّ محتوى هذه الصفحة مرخّص بموجب ترخيص Creative Commons Attribution 4.0 ما لم يُنصّ على خلاف ذلك، ونماذج الرموز مرخّصة بموجب ترخيص Apache 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."]]