با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
این راهنمای شروع سریع نحوه راهاندازی یک برنامه خط فرمان ساده و PHP را توضیح میدهد که درخواستهایی را به YouTube Data API ارسال میکند. این شروع سریع در واقع نحوه ایجاد دو درخواست API را توضیح می دهد:
برای بازیابی اطلاعات کانال YouTube GoogleDevelopers از یک کلید API استفاده می کنید که برنامه شما را شناسایی می کند.
شما از شناسه مشتری OAuth 2.0 برای ارسال یک درخواست مجاز استفاده می کنید که اطلاعات مربوط به کانال YouTube شما را بازیابی می کند.
پیش نیازها
برای اجرای این شروع سریع، به موارد زیر نیاز دارید:
PHP 5.4 یا بالاتر با رابط خط فرمان (CLI) و پسوند JSON نصب شده است.
اگر قبلاً کتابخانه سرویس گیرنده را نصب کردهاید، توصیه میکنیم آن را بهروزرسانی کنید تا مطمئن شوید که بهروزترین کلاسها را برای کتابخانهای که آزمایش میکنید دارید:
ایجاد یک کلید API برای ایجاد درخواست های API که نیازی به مجوز کاربر ندارند از کلید API استفاده خواهید کرد. به عنوان مثال، برای بازیابی اطلاعات یک کانال عمومی YouTube نیازی به مجوز کاربر ندارید.
ایجاد شناسه مشتری OAuth 2.0 نوع برنامه را روی Other تنظیم کنید. برای درخواست هایی که نیاز به مجوز کاربر دارند، باید از اعتبارنامه OAuth 2.0 استفاده کنید. به عنوان مثال، برای بازیابی اطلاعات مربوط به کانال یوتیوب کاربر تأیید شده فعلی، به مجوز کاربر نیاز دارید.
فایل JSON را که حاوی اطلاعات کاربری OAuth 2.0 شما است دانلود کنید. این فایل دارای نامی مانند client_secret_CLIENTID.json است که در آن CLIENTID شناسه مشتری پروژه شما است.
مرحله 2: نمونه را تنظیم و اجرا کنید
از ویجت APIs Explorer در پانل کناری برای دریافت کد نمونه برای بازیابی اطلاعات مربوط به کانال YouTube GoogleDevelopers استفاده کنید. این درخواست از یک کلید API برای شناسایی برنامه شما استفاده می کند و نیازی به مجوز کاربر یا مجوزهای خاصی از کاربر در حال اجرا نمونه ندارد.
در آن صفحه، بخش «موارد استفاده متداول» حاوی جدولی است که چندین روش رایج استفاده از این روش را توضیح میدهد. اولین لیست در جدول برای لیست کردن نتایج بر اساس شناسه کانال است.
روی نماد کد اولین فهرست کلیک کنید تا APIs Explorer تمام صفحه باز و پر شود.
سمت چپ APIs Explorer تمام صفحه موارد زیر را نشان می دهد:
در زیر سربرگ پارامترهای درخواست ، لیستی از پارامترهایی وجود دارد که این روش از آنها پشتیبانی می کند. مقادیر پارامتر part و id باید تنظیم شوند. مقدار پارامتر id ، UC_x5XG1OV2P6uZZ5FSM9Ttw ، شناسه کانال GoogleDevelopers YouTube است.
در زیر پارامترها، بخشی به نام Credentials وجود دارد. منوی کشویی در آن بخش باید مقدار کلید API را نمایش دهد. APIs Explorer به طور پیشفرض از اعتبارنامههای نمایشی استفاده میکند تا شروع به کار را آسانتر کند. اما شما از کلید API خود برای اجرای نمونه به صورت محلی استفاده خواهید کرد.
سمت راست APIs Explorer تمام صفحه برگه هایی را با نمونه کد به زبان های مختلف نشان می دهد. تب PHP را انتخاب کنید.
نمونه کد را کپی کرده و در فایلی به نام example.php ذخیره کنید.
در نمونه ای که دانلود کردید، رشته YOUR_API_KEY را پیدا کنید و آن را با کلید API که در مرحله 1 این شروع سریع ایجاد کردید جایگزین کنید.
نمونه را از خط فرمان اجرا کنید. در پوشه کاری خود، اجرا کنید:
php example.php
نمونه باید درخواست را اجرا کند و پاسخ STDOUT را چاپ کند.
مرحله 3: یک درخواست مجاز را اجرا کنید
در این مرحله، نمونه کد خود را طوری تغییر میدهید که به جای بازیابی اطلاعات مربوط به کانال YouTube GoogleDevelopers، اطلاعات مربوط به کانال YouTube شما را بازیابی کند. این درخواست نیاز به مجوز کاربر دارد.
در بخش «موارد استفاده متداول»، روی نماد کد فهرست سوم در جدول کلیک کنید. این مورد استفاده فراخوانی متد list برای «کانال من» است.
مجدداً، در سمت چپ APIs Explorer تمام صفحه، لیستی از پارامترها را مشاهده خواهید کرد که به دنبال آن بخش Credentials قرار دارد. با این حال، دو تغییر نسبت به مثالی که در آن اطلاعات مربوط به کانال GoogleDevelopers را بازیابی کردید، وجود دارد:
در قسمت پارامترها به جای اینکه مقدار پارامتر id تنظیم شود، مقدار پارامتر mine باید روی true تنظیم شود. این به سرور API دستور می دهد تا اطلاعات مربوط به کانال کاربر تأیید شده فعلی را بازیابی کند.
در بخش اعتبارنامهها ، منوی کشویی باید گزینه Google OAuth 2.0 را انتخاب کند.
علاوه بر این، اگر روی پیوند Show scopes کلیک کنید، محدوده https://www.googleapis.com/auth/youtube.readonly باید بررسی شود.
مانند مثال قبلی، تب PHP را انتخاب کنید، نمونه کد را کپی کنید و آن را در example.php ذخیره کنید.
در کد، رشته YOUR_CLIENT_SECRET_FILE.json را پیدا کنید و آن را با محل فایل مخفی کلاینت که در مرحله 1 این شروع سریع دانلود کرده اید جایگزین کنید.
نمونه را از خط فرمان اجرا کنید. در پوشه کاری خود، اجرا کنید:
php example.php
URL را از کنسول کپی کرده و در مرورگر خود باز کنید.
اگر قبلاً وارد حساب Google خود نشدهاید، از شما خواسته میشود که وارد شوید. اگر به چندین حساب Google وارد شدهاید، از شما خواسته میشود یک حساب را برای استفاده برای مجوز انتخاب کنید.
روی دکمه کلیک کنید تا برنامه شما به محدوده های مشخص شده در نمونه کد شما دسترسی پیدا کند.
کد احراز هویت را از مرورگر کپی کرده و در ترمینال خود قرار دهید. سپس می توانید برگه مرورگر مورد استفاده برای جریان احراز هویت را ببندید.
تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-21 بهوقت ساعت هماهنگ جهانی."],[[["\u003cp\u003eThis guide demonstrates setting up a PHP command-line application to interact with the YouTube Data API.\u003c/p\u003e\n"],["\u003cp\u003eThe guide covers making two distinct API requests: one using an API key to get information about the GoogleDevelopers YouTube channel, and another using OAuth 2.0 to access data about your own YouTube channel.\u003c/p\u003e\n"],["\u003cp\u003eTo run this quickstart, you'll need PHP 5.4 or greater, the Composer dependency management tool, and the Google APIs Client Library for PHP.\u003c/p\u003e\n"],["\u003cp\u003eThe setup involves creating an API key for public data requests and an OAuth 2.0 client ID for requests needing user authorization, both within the Google API Console.\u003c/p\u003e\n"],["\u003cp\u003eThe guide will also help with running an authorized request by using the \u003cem\u003emine\u003c/em\u003e parameter set to true, which fetches information for the authenticated user's channel.\u003c/p\u003e\n"]]],["This guide demonstrates using the YouTube Data API with PHP. It outlines two types of API requests: one using an API key to retrieve public channel data, and another using OAuth 2.0 for authorized access to a user's own channel data. The process involves setting up a project in the API Console, enabling the YouTube Data API, and generating both an API key and an OAuth 2.0 client ID. Sample code is provided and instructions are given for updating parameters, integrating credentials and running the scripts to retrieve information from the API.\n"],null,["# PHP Quickstart\n\nThis quickstart guide explains how to set up a simple, PHP\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- PHP 5.4 or greater with the command-line interface (CLI) and JSON extension installed.\n- The Composer dependency management tool [installed globally](https://getcomposer.org/doc/00-intro.md#globally) {: target=\"_blank\"}\n- The Google APIs Client Library for PHP:\n - If you have not previously installed the client library: \n\n ```\n composer require google/apiclient:^2.0\n ```\n - If you previously installed the client library, we recommend updating it to ensure that you have the most up-to-date classes for the library you are testing: \n\n ```\n composer update google/apiclient --with-dependencies\n ```\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 **PHP** tab.\n\n5. Copy the code sample and save it in a file named\n `example.php`.\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 `php example.php`\n\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 **PHP** tab,\n copy the code sample, and save it to `example.php`.\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 `php example.php`\n\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 PHP documentation](/api-client-library/php)\n- [Google APIs Client Library for PHP on GitHub](https://github.com/googleapis/google-api-php-client) and [autogenerated classes](https://github.com/googleapis/google-api-php-client-services/tree/master/src/Google/Service) for the YouTube API. (On the page, find the `YouTube` folder and the `YouTube.php` file.\n- [YouTube Data API reference documentation](/youtube/v3/docs)"]]