基于YouTube API .NET示例修改视频描述遇403禁止错误求助
Root Cause
The 403 Forbidden error you're hitting is almost certainly tied to an incorrect OAuth 2.0 scope configuration. The original sample code uses a read-only scope, which only lets you view account data—not modify it. When you try to edit a video's description with this restricted scope, YouTube's API rejects the request outright.
Even though you switched to YoutubeForceSsl, if you didn't clear old cached auth tokens or confirm the scope's permissions, the issue persists.
Step-by-Step Solution
1. Confirm & Update the OAuth Scope
First, make sure you're using a scope that grants write access to video metadata. For editing descriptions, use either:
YouTubeService.Scope.Youtube: Full access to the authenticated user's YouTube account (covers all write operations)YouTubeService.Scope.YoutubeForceSsl: SSL-only restricted access that still allows modifying video metadata (safer for production)
Here's the corrected authorization code snippet:
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) { credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, // Use a write-enabled scope new[] { YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None, new FileDataStore("YouTube.Auth.Store") ); }
2. Clear Cached Auth Tokens
OAuth tokens are cached in the FileDataStore you specified. Old tokens issued with read-only permissions won't work with the new scope, so you need to delete the cache:
- On Windows: Navigate to
%APPDATA%\Roamingand delete theYouTube.Auth.Storefolder - On macOS/Linux: Go to
~/.configand remove theYouTube.Auth.Storefolder
This forces a fresh authorization flow, ensuring you get a token with the correct write permissions.
3. Verify Project & Quota Setup
Double-check these critical details to rule out other 403 triggers:
- Ensure the YouTube Data API v3 is enabled in your Google Cloud project (check under APIs & Services > Enabled APIs & Services)
- Confirm you haven't exceeded your API quota (view limits under APIs & Services > Quotas)
- If your app is in testing mode, make sure the authenticated user is added as a test user in the OAuth consent screen settings
4. Implement the Description Update Correctly
Once your auth is sorted, use this example to update a video's description properly:
var youtubeService = new YouTubeService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Your App Name" }); // Fetch the target video (replace with your video ID) var video = youtubeService.Videos.List("snippet").SetId("YOUR_VIDEO_ID").Execute().Items.First(); // Update the description video.Snippet.Description = "Your updated video description text here"; // Send the update request (specify "snippet" since we're modifying that part of the video data) var updateRequest = youtubeService.Videos.Update(video, "snippet"); updateRequest.Execute();
Common Pitfalls to Avoid
- Forgetting to re-authenticate after scope changes: Cached tokens retain old permissions, so clearing the cache is mandatory.
- Using the wrong "part" in the update request: The description lives in the
snippetpart, so you must include that in theVideos.Updatecall. - Overlooking quota limits: Even with correct scopes, hitting your API quota will also trigger a 403 error.
内容的提问来源于stack exchange,提问作者Vitalis Hommel

