You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用C#及API Key获取YouTube账号私有列表视频并接入网站

Fixing Access to Private YouTube Playlists (Instead of API Key)

Hey there, the issue here is straightforward: API Keys only work for accessing public YouTube data—they don’t authenticate you as the owner of the YouTube account, so YouTube won’t hand over private playlists or videos to an API Key request. To access your own private content, you need to use OAuth 2.0 authorization instead, which verifies your identity as the account holder.

Here’s how to adjust your code and setup to make this work:

Step 1: Set Up OAuth 2.0 Credentials

First, head to your Google Cloud Console project (the same one where you created your API Key):

  • Go to the "APIs & Services" > "Credentials" page
  • Click "Create Credentials" > "OAuth client ID"
  • Select the appropriate application type (e.g., "Desktop app" if this is a local script)
  • Download the generated client_secret.json file and place it in your project directory

Step 2: Update Your Code to Use OAuth

You’ll need to use the Google Auth library to handle the OAuth flow. Here’s a revised version of your code that supports private playlist access:

using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using System;
using System.IO;
using System.Threading;

class Program
{
    static void Main(string[] args)
    {
        // Define the scopes we need (readonly access is enough for viewing private content)
        string[] scopes = { YouTubeService.Scope.YoutubeReadonly };
        string clientSecretPath = "client_secret.json"; // Path to your downloaded JSON file

        // Initialize OAuth credentials
        UserCredential credential;
        using (var stream = new FileStream(clientSecretPath, FileMode.Open, FileAccess.Read))
        {
            string credPath = "token.json"; // This stores cached credentials after first login
            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                scopes,
                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;
        }

        // Create YouTube service with OAuth credentials
        var yt = new YouTubeService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Your App Name" // Replace with your app's name
        });

        // To get private playlists, use Playlists.List instead of Search.List (Search won't return private content)
        var playlistsRequest = yt.Playlists.List("snippet,contentDetails");
        playlistsRequest.Mine = true; // Tells YouTube to return YOUR playlists (including private ones)

        var playlistsResult = playlistsRequest.Execute();
        foreach (var playlist in playlistsResult.Items)
        {
            Console.WriteLine($"Playlist ID: {playlist.Id}");
            Console.WriteLine($"Playlist Title: {playlist.Snippet.Title}");
            Console.WriteLine($"Is Private: {playlist.Status.PrivacyStatus == "private"}");

            // Optional: Get videos inside the private playlist
            var playlistItemsRequest = yt.PlaylistItems.List("snippet");
            playlistItemsRequest.PlaylistId = playlist.Id;
            var playlistItemsResult = playlistItemsRequest.Execute();

            foreach (var item in playlistItemsResult.Items)
            {
                Console.WriteLine($"  Video ID: {item.Snippet.ResourceId.VideoId}");
                Console.WriteLine($"  Video Title: {item.Snippet.Title}");
            }
        }
    }
}

Key Notes:

  • Mine = true: This is critical—it tells the YouTube API to return playlists owned by the authenticated user (you), including private ones. The Search.List method you were using won’t return private content, even with OAuth, so we use Playlists.List instead.
  • Scopes: We used YoutubeReadonly which is sufficient for viewing private content. If you need to modify playlists later, you can use YouTubeService.Scope.Youtube instead.
  • First Run: When you run this code for the first time, a browser window will pop up asking you to log in to your YouTube account and grant access to your app. After that, your credentials will be cached in token.json so you won’t need to log in again.

Once you make these changes, you should be able to access your private playlists and their videos without issues.

内容的提问来源于stack exchange,提问作者sachin K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:37:24