تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
يوضّح دليل البدء السريع هذا كيفية إعداد تطبيق بسيط يعمل من سطر الأوامر بلغة Python ويرسل طلبات إلى YouTube Data API. يوضّح هذا الدليل السريع كيفية إرسال طلبَين إلى واجهة برمجة التطبيقات:
ستستخدم مفتاح واجهة برمجة تطبيقات يحدّد تطبيقك لاسترداد معلومات حول قناة GoogleDevelopers على YouTube.
ستستخدم معرّف عميل OAuth 2.0 لإرسال طلب مفوّض يسترد معلومات حول قناتك على YouTube.
المتطلبات الأساسية
لتشغيل هذا التشغيل السريع، ستحتاج إلى ما يلي:
الإصدار 2.7 أو الإصدار 3.5 أو الإصدارات الأحدث من Python
إنشاء مفتاح واجهة برمجة التطبيقات
ستستخدم مفتاح واجهة برمجة التطبيقات لإرسال طلبات إلى واجهة برمجة التطبيقات لا تتطلّب تفويضًا من المستخدم. على سبيل المثال، لا تحتاج إلى إذن المستخدم لاسترداد معلومات حول قناة YouTube عامة.
إنشاء معرّف عميل OAuth 2.0
اضبط نوع التطبيق على غير ذلك. يجب استخدام بيانات اعتماد OAuth 2.0 للطلبات التي تتطلّب تفويض المستخدم. على سبيل المثال،
تحتاج إلى إذن من المستخدم لاسترداد معلومات حول قناة YouTube الخاصة بالمستخدم الذي تم التحقّق من هويته حاليًا.
نزِّل ملف JSON الذي يحتوي على بيانات اعتماد OAuth 2.0. يحتوي الملف على اسم مثل client_secret_CLIENTID.json، حيث يمثّل CLIENTID معرّف العميل لمشروعك.
الخطوة 2: إعداد العيّنة وتشغيلها
استخدِم أداة APIs Explorer في اللوحة الجانبية للحصول على نموذج رمز برمجي لاسترداد معلومات حول قناة GoogleDevelopers على YouTube. يستخدم هذا الطلب مفتاح واجهة برمجة تطبيقات للتعرّف على تطبيقك، ولا يتطلّب تفويضًا من المستخدم أو أي أذونات خاصة من المستخدم الذي يشغّل النموذج.
افتح مستندات طريقة channels.list في واجهة برمجة التطبيقات.
في تلك الصفحة، يحتوي قسم "حالات الاستخدام الشائعة" على جدول يوضّح عدة طرق شائعة لاستخدام الطريقة. تخصّص القائمة الأولى في الجدول نتائج البحث حسب معرّف القناة.
انقر على رمز الرمز البرمجي الخاص بالإدراج الأول لفتح أداة
Fullscreen APIs Explorer وتعبئتها.
يعرض الجانب الأيسر من "مستكشف واجهات برمجة التطبيقات لملء الشاشة" ما يلي:
أسفل عنوان مَعلمات الطلب، تظهر قائمة بالمَعلمات التي تتوافق مع الطريقة. يجب ضبط قيمتَي المَعلمتَين part وid. قيمة المَعلمة id، أي UC_x5XG1OV2P6uZZ5FSM9Ttw، هي
المعرّف الخاص بقناة GoogleDevelopers على YouTube.
أسفل المَعلمات، هناك قسم باسم بيانات الاعتماد. يجب أن تعرض القائمة المنسدلة في هذا القسم القيمة مفتاح واجهة برمجة التطبيقات. يستخدم
APIs Explorer بيانات اعتماد تجريبية تلقائيًا لتسهيل عملية البدء. ولكنّك ستستخدم مفتاح واجهة برمجة التطبيقات الخاص بك لتشغيل النموذج محليًا.
يعرض الجانب الأيسر من "مستكشف واجهات برمجة التطبيقات بملء الشاشة" علامات تبويب تتضمّن عيّنات من الرموز البرمجية بلغات مختلفة. انقر على علامة التبويب Python.
انسخ نموذج الرمز واحفظه في ملف باسم
example.py.
في النموذج الذي نزّلته، ابحث عن السلسلة YOUR_API_KEY واستبدلها بمفتاح واجهة برمجة التطبيقات الذي أنشأته في الخطوة 1 من هذا الدليل السريع.
تشغيل النموذج من سطر الأوامر في دليل العمل، نفِّذ الأمر التالي:
python example.py
يجب أن ينفّذ النموذج الطلب ويطبع الردّ على STDOUT.
الخطوة 3: تنفيذ طلب معتمد
في هذه الخطوة، ستعدّل نموذج الرمز البرمجي لكي يسترد معلومات حول قناتك على YouTube بدلاً من استرداد معلومات حول قناة GoogleDevelopers على YouTube. يتطلّب هذا الطلب تفويض المستخدم.
ارجع إلى مستندات طريقة channels.list الخاصة بواجهة برمجة التطبيقات.
في قسم "حالات الاستخدام الشائعة"، انقر على رمز الرمز البرمجي الخاص بالإدخال الثالث في الجدول. حالة الاستخدام هذه هي استدعاء الطريقة list للقناة "my
channel".
مرة أخرى، في الجانب الأيمن من "مستكشف واجهات برمجة التطبيقات بملء الشاشة"، ستظهر لك قائمة بالمعلمات متبوعة بقسم بيانات الاعتماد. ومع ذلك، هناك تغييران عن المثال الذي استرجعت فيه معلومات حول قناة GoogleDevelopers:
في قسم المَعلمات، بدلاً من ضبط قيمة المَعلمة id، يجب ضبط قيمة المَعلمة mine على true. يطلب هذا النطاق من خادم واجهة برمجة التطبيقات استرداد معلومات حول قناة المستخدم الذي تمّت مصادقته حاليًا.
في قسم بيانات الاعتماد، يجب أن تختار القائمة المنسدلة الخيار Google OAuth 2.0.
بالإضافة إلى ذلك، إذا نقرت على الرابط عرض النطاقات، يجب أن يكون النطاق https://www.googleapis.com/auth/youtube.readonly محدّدًا.
كما هو الحال مع المثال السابق، اختَر علامة التبويب Python،
وانسخ نموذج الرمز البرمجي، واحفظه في example.py.
في الرمز، ابحث عن السلسلة YOUR_CLIENT_SECRET_FILE.json واستبدِلها بموقع ملف سر العميل الذي نزّلته في الخطوة 1 من هذا الدليل السريع.
تشغيل النموذج من سطر الأوامر في دليل العمل، نفِّذ الأمر التالي:
python example.py
انسخ عنوان URL من وحدة التحكّم وافتحه في المتصفّح.
إذا لم تكن مسجِّلاً الدخول إلى حسابك على Google، سيُطلب منك تسجيل الدخول. إذا كنت مسجّلاً الدخول إلى عدة حسابات على Google، سيُطلب منك اختيار حساب واحد لاستخدامه في عملية التفويض.
انقر على الزر لمنح تطبيقك إذن الوصول إلى النطاقات المحدّدة في نموذج الرمز البرمجي.
انسخ رمز المصادقة من المتصفّح والصِقه في نافذة الأوامر. يمكنك بعد ذلك إغلاق علامة تبويب المتصفّح المستخدَمة في عملية المصادقة.
يجب طباعة الردّ من واجهة برمجة التطبيقات مرة أخرى في STDOUT.
تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)
[null,null,["تاريخ التعديل الأخير: 2025-08-21 (حسب التوقيت العالمي المتفَّق عليه)"],[[["\u003cp\u003eThis guide walks through setting up a Python command-line application to interact with the YouTube Data API.\u003c/p\u003e\n"],["\u003cp\u003eYou'll learn how to use an API key to retrieve public information, such as details about the GoogleDevelopers YouTube channel.\u003c/p\u003e\n"],["\u003cp\u003eThe guide also covers using OAuth 2.0 for authorized requests, enabling you to retrieve information about your own YouTube channel.\u003c/p\u003e\n"],["\u003cp\u003ePrerequisites include Python 2.7 or 3.5+, \u003ccode\u003epip\u003c/code\u003e, the Google APIs Client Library for Python, and the \u003ccode\u003egoogle-auth-oauthlib\u003c/code\u003e and \u003ccode\u003egoogle-auth-httplib2\u003c/code\u003e libraries.\u003c/p\u003e\n"],["\u003cp\u003eThe process includes setting up your project in the API Console, creating an API key and OAuth 2.0 credentials, and running sample code to make API requests.\u003c/p\u003e\n"]]],["This guide demonstrates using the YouTube Data API with Python. It covers two core actions: retrieving channel information using an API key and making an authorized request for your own channel data using OAuth 2.0. Key steps include setting up a project in the API Console, enabling the YouTube Data API, creating an API key and OAuth 2.0 credentials. The guide provides instructions for running code samples that retrieve information, replacing placeholder strings with personal credentials, and completing user authorization in the browser.\n"],null,["# Python Quickstart\n\nThis quickstart guide explains how to set up a simple, Python\ncommand-line application that makes requests to the YouTube Data API. This\nquickstart actually explains how to make two API requests:\n\n1. You will use an API key, which identifies your application, to retrieve information about the GoogleDevelopers YouTube channel.\n2. You will use an OAuth 2.0 client ID to submit an *authorized* request that retrieves information about your own YouTube channel.\n\n| **Note:** More generally, you can follow the instructions for the first example for any use case that uses an API key or the instructions for the second example for any use case that requires authorization using OAuth 2.0. See the [use cases and code samples tool](/youtube/v3/code_samples/code_snippets) for more examples.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you'll need:\n\n- Python 2.7 or Python 3.5+\n\n- The `pip` package management tool\n\n- The Google APIs Client Library for Python:\n\n pip install --upgrade google-api-python-client\n\n- The `google-auth-oauthlib` and `google-auth-httplib2` libraries for user\n authorization.\n\n pip install --upgrade google-auth-oauthlib google-auth-httplib2\n\nStep 1: Set up your project and credentials\n-------------------------------------------\n\nCreate or select a project in the [API Console](https://console.cloud.google.com/). Complete the following tasks in the API Console for your project:\n\n1. In the [library panel](https://console.developers.google.com/apis/library),\n search for the YouTube Data API v3. Click into the listing for that API and\n make sure the API is enabled for your project.\n\n2. In the [credentials\n panel](https://console.developers.google.com/apis/credentials),\n create two credentials:\n\n 1. **Create an API key**\n You will use the API key to make API requests that do\n not require user authorization. For example, you do not need user\n authorization to retrieve information about a public YouTube channel.\n\n 2. **Create an OAuth 2.0 client ID**\n\n Set the application type to **Other**. You need to use OAuth 2.0\n credentials for requests that require user authorization. For example,\n you need user authorization to retrieve information about the currently\n authenticated user's YouTube channel.\n\n Download the JSON file that contains your OAuth 2.0 credentials. The\n file has a name like `client_secret_CLIENTID.json`, where `CLIENTID` is\n the client ID for your project.\n\nStep 2: Set up and run the sample\n---------------------------------\n\nUse the APIs Explorer widget in the side panel to obtain sample code for\nretrieving information about the GoogleDevelopers YouTube channel. This request\nuses an API key to identify your application, and it does not require user\nauthorization or any special permissions from the user running the sample.\n\n1. Open the documentation for the API's [channels.list](/youtube/v3/docs/channels/list) method.\n2. On that page, the \"Common use cases\" section contains a table that explains\n several common ways that the method is used. The first listing in the table\n is for listing results by channel ID.\n\n Click the code symbol for the first listing to open and populate the\n fullscreen APIs Explorer.\n\n3. The left side of the fullscreen APIs Explorer shows the following:\n\n 1. Below the **Request parameters** header, there is a list of parameters\n that the method supports. The `part` and `id` parameter values should\n be set. The `id` parameter value, `UC_x5XG1OV2P6uZZ5FSM9Ttw`, is the\n ID for the GoogleDevelopers YouTube channel.\n\n 2. Below the parameters, there is a section named **Credentials** . The\n pulldown menu in that section should display the value **API key**. The\n APIs Explorer uses demo credentials by default to make it easier to get\n started. But you'll use your own API key to run the sample locally.\n\n4. The right side of the fullscreen APIs Explorer shows tabs with code samples\n in different languages. Select the **Python** tab.\n\n5. Copy the code sample and save it in a file named\n `example.py`.\n\n6. In the sample that you downloaded, find the `YOUR_API_KEY` string and\n replace that with the API key that you created in step 1 of this quickstart.\n\n7. Run the sample from the command line. In your working directory, run:\n\n\n `python example.py`\n8. The sample should execute the request and print the response to `STDOUT`.\n\nStep 3: Run an authorized request\n---------------------------------\n\nIn this step, you'll modify your code sample so that instead of retrieving\ninformation about the GoogleDevelopers YouTube channel, it retrieves information\nabout *your* YouTube channel. This request does require user authorization.\n\n1. Go back to the documentation for the API's\n [channels.list](/youtube/v3/docs/channels/list) method.\n\n2. In the \"Common use cases\" section, click the code symbol for the third\n listing in the table. That use case is to call the `list` method for \"my\n channel.\"\n\n3. Again, in the left side of the fullscreen APIs Explorer, you will see a\n list of parameters followed by the **Credentials** section. However, there\n are two changes from the example where you retrieved information about the\n GoogleDevelopers channel:\n\n 1. In the parameters section, instead of the `id` parameter value being\n set, the `mine` parameter value should be set to `true`. This instructs\n the API server to retrieve information about the currently authenticated\n user's channel.\n\n 2. In the **Credentials** section, the pulldown menu should select the\n option for **Google OAuth 2.0**.\n\n In addition, if you click the **Show scopes** link, the\n **https://www.googleapis.com/auth/youtube.readonly** scope should be\n checked.\n\n4. As with the previous example, select the **Python** tab,\n copy the code sample, and save it to `example.py`.\n\n\n In the code, find the `YOUR_CLIENT_SECRET_FILE.json` string and replace\n it with the location of the client secret file you downloaded in step 1\n of this quickstart.\n5. Run the sample from the command line. In your working directory, run:\n\n\n `python example.py`\n6. \u003cbr /\u003e\n\n\n Copy the URL from the console and open it in your browser.\n\n If you are not already logged into your Google account, you will be\n prompted to log in. If you are logged into multiple Google accounts, you\n will be asked to select one account to use for the authorization.\n7. Click the button to grant your application access to the scopes specified in\n your code sample.\n\n8. \u003cbr /\u003e\n\n\n Copy the auth code from the browser and paste it into your terminal. You can\n then close the browser tab used for the auth flow.\n\n The API response should again be printed to `STDOUT`.\n\nFurther reading\n---------------\n\n- [Google Developers Console help documentation](/console/help/new)\n- [Google APIs Client Library for Python documentation](/api-client-library/python)\n- [YouTube Data API PyDoc documentation](https://developers.google.com/resources/api-libraries/documentation/youtube/v3/python/latest/)\n- [YouTube Data API reference documentation](/youtube/v3/docs)"]]