我們正在更新 Data API,以符合 YouTube 計算 Shorts 觀看次數的方式。
瞭解詳情
實作:訂閱
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
以下範例說明如何使用 YouTube Data API (第 3 版) 執行與訂閱相關的函式。
擷取頻道的訂閱項目
新增訂閱項目
刪除訂閱項目
擷取授權使用者頻道的訂閱者清單
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2024-11-23 (世界標準時間)。
[null,null,["上次更新時間:2024-11-23 (世界標準時間)。"],[[["\u003cp\u003eThe YouTube Data API (v3) allows retrieving a list of subscriptions for a channel using the \u003ccode\u003esubscriptions.list\u003c/code\u003e method, either for the authenticated user's channel (using \u003ccode\u003emine=true\u003c/code\u003e) or for another channel (using \u003ccode\u003echannelId\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eTo add a channel subscription, you can use the \u003ccode\u003esubscriptions.insert\u003c/code\u003e method, providing the \u003ccode\u003eyoutube#channel\u003c/code\u003e kind and the target channel's ID in the request body's \u003ccode\u003esnippet.resourceId\u003c/code\u003e property, while also needing to be authorized using OAuth 2.0.\u003c/p\u003e\n"],["\u003cp\u003eDeleting a channel subscription involves first retrieving the subscription list using \u003ccode\u003esubscriptions.list\u003c/code\u003e to find the subscription ID, and then using \u003ccode\u003esubscriptions.delete\u003c/code\u003e with that ID to remove the subscription, which requires OAuth 2.0 authorization.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003esubscriptions.list\u003c/code\u003e method can also be utilized to retrieve a list of channels that subscribe to the currently authenticated user's channel by setting the \u003ccode\u003emySubscribers\u003c/code\u003e parameter to \u003ccode\u003etrue\u003c/code\u003e, while needing to be authorized using OAuth 2.0.\u003c/p\u003e\n"]]],["The YouTube Data API (v3) manages subscriptions via the `subscriptions` resource. To retrieve subscriptions, use `subscriptions.list`, setting `mine` to `true` for the authenticated user or `channelId` for others. Adding subscriptions involves `subscriptions.insert`, specifying `youtube#channel` and the target `channelId`. To delete, use `subscriptions.delete` after using `subscriptions.list` to identify the subscription `id`. Retrieve a list of a channel subscribers by calling `subscriptions.list` and setting the `mySubscribers` to `true`. All actions except by channelId require OAuth 2.0.\n"],null,["# Implementation: Subscriptions\n\nThe following examples show how to use the YouTube Data API (v3) to perform functions related to subscriptions.\n\nRetrieve a channel's subscriptions\n----------------------------------\n\nCall the [subscriptions.list](/youtube/v3/docs/subscriptions/list) method to retrieve subscriptions for a particular channel. There are two ways to identify the channel:\n\n- To retrieve the currently authenticated user's subscriptions, set the [mine](/youtube/v3/docs/subscriptions/list#mine) parameter's value to `true`. Note that a request that uses the `mine` parameter must be authorized using OAuth 2.0.\n\n ```\n https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list?\n part=snippet,contentDetails\n &mine=true\n ```\n- To retrieve subscriptions for any other channel, set the [channelId](/youtube/v3/docs/subscriptions/list#channelId) parameter's value to that channel's unique YouTube channel ID. The example below retrieves a list of channels subscribed to by the TED channel on YouTube.\n\n ```\n https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list?\n part=snippet,contentDetails\n &channelId=UCAuUUnT6oDeKwE6v1NGQxug\n ```\n\n **Note:** The API returns a `403 (Forbidden)` HTTP response code if the specified channel does not publicly expose its subscriptions and the request is not authorized by the channel's owner.\n\nSee the [subscriptions.list](/youtube/v3/docs/subscriptions/list#usage) method's documentation for code samples.\n\nAdd a subscription\n------------------\n\nCall the [subscriptions.insert](/youtube/v3/docs/subscriptions/insert) method to add a channel subscription. This request must be authorized using OAuth 2.0. The request body is a [subscription](/youtube/v3/docs/subscriptions) resource that sets the following values:\n\n\u003cbr /\u003e\n\n- The [snippet.resourceId.kind](/youtube/v3/docs/subscriptions#snippet.resourceId.kind) contains the value `youtube#channel`.\n- The [snippet.resourceId.channelId](/youtube/v3/docs/subscriptions#snippet.resourceId.channelId) property identifies the channel that is being subscribed to. The property value is a unique YouTube channel ID. The channel ID could be obtained in multiple ways, including calling the [channels.list](/youtube/v3/docs/channels/list) method or retrieving [search results for channels](/youtube/v3/docs/search/list).\n\n\u003cbr /\u003e\n\nThe API request below subscribes you to the TED channel on YouTube: \n\n```\nhttps://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.insert?\n part=snippet\n```\n\nThe request body is: \n\n```\n{\n \"snippet\": {\n \"resourceId\": {\n \"kind\": \"youtube#channel\",\n \"videoId\": \"UCAuUUnT6oDeKwE6v1NGQxug\"\n }\n }\n}\n```\n\nSee the [subscriptions.insert](/youtube/v3/docs/subscriptions/insert#usage) method's documentation for code samples.\n\nDelete a subscription\n---------------------\n\nThis example deletes a subscription. This request must be authorized using OAuth 2.0. This example has two steps:\n\n- **Step 1: Retrieve the subscriptions for the authenticated user's channel**\n\n Call the [subscriptions.list](/youtube/v3/docs/subscriptions/list) method to retrieve the list of subscriptions. The example above for retrieving a channel's subscriptions explains how to make this request.\n\n The application calling the API could process the API response to display a list of subscriptions, using each subscription's ID as a key. In the response, each item's `id` property identifies the subscription ID that uniquely identifies the corresponding subscription. You will use that value to remove an item from the list in the next step.\n- **Step 2: Delete a subscription**\n\n Call the [subscriptions.delete](/youtube/v3/docs/subscriptions/delete) method to delete a subscription. Set the request's [id](/youtube/v3/docs/subscriptions/delete#id) parameter to the subscription ID for the subscription that you want to remove. This request must be authorized using OAuth 2.0.\n\n To complete the request in the APIs Explorer, you need to set the `id` property's value. \n\n ```\n https://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.delete?\n id=SUBSCRIPTION_ID\n ```\n\nSee the [subscriptions.delete](/youtube/v3/docs/subscriptions/delete#usage) method's documentation for code samples.\n\nRetrieve a list of subscribers to the authorized user's channel\n---------------------------------------------------------------\n\nTo retrieve a list of channels that subscribe to the currently authenticated user's channel, call the `subscriptions.list` method and set the [mySubscribers](/youtube/v3/docs/subscriptions/list#mySubscribers) parameter's value to `true`. The request must be authorized using OAuth 2.0. \n\n```\nhttps://developers.google.com/apis-explorer/#p/youtube/v3/youtube.subscriptions.list?\n part=snippet,contentDetails\n &mySubscribers=true\n```"]]