如何使用C#及API Key获取YouTube账号私有列表视频并接入网站
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.jsonfile 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. TheSearch.Listmethod you were using won’t return private content, even with OAuth, so we usePlaylists.Listinstead.- Scopes: We used
YoutubeReadonlywhich is sufficient for viewing private content. If you need to modify playlists later, you can useYouTubeService.Scope.Youtubeinstead. - 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.jsonso 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

