我一整天都在尝试将视频删除/添加到我的 YouTube 播放列表中。我使用的是 YouTube 数据 API v.3.0。对于.NET C#。 我已经在 Google 开发者控制台中创建了一个项目并获取了我的客户端 secret JSON 文件。另外,我用于获取列表项的代码工作正常,这意味着只有 PUT 操作未按预期工作。我使用了与 Google 开发人员站点代码示例中几乎相同的代码。
认证方式:
private async Task<YouTubeService> GetYouTubeService(string userEmail)
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[]
{
YouTubeService.Scope.Youtube,
YouTubeService.Scope.Youtubepartner,
YouTubeService.Scope.YoutubeUpload,
YouTubeService.Scope.YoutubepartnerChannelAudit,
YouTubeService.Scope.YoutubeReadonly
},
userEmail,
CancellationToken.None,
new FileDataStore(this.GetType().ToString()));
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
return youtubeService;
}
将视频添加到播放列表代码:
private async Task AddSongToPlaylistAsync(string userEmail, string songId, string playlistId)
{
var youtubeService = await this.GetYouTubeService(userEmail);
var newPlaylistItem = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = playlistId;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.Kind = "youtube#video";
newPlaylistItem.Snippet.ResourceId.VideoId = songId;
newPlaylistItem = await youtubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();
}
这是我尝试将新视频添加到指定播放列表时收到的消息:
Google.Apis.Requests.RequestError Insufficient Permission [403] Errors [ Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global] ]
我真的很感激任何可用的帮助,因为我没有找到任何有用的谷歌搜索。 先感谢您!
最佳答案
我遇到了同样的问题,但如果有人在将视频添加到播放列表时遇到低效权限问题,您将需要拥有 YouTubeService.Scope.Youtube
(看起来您已经有)。
var scopes = new[]
{
YouTubeService.Scope.Youtube
};
但是,如果您在授予权限后添加了范围,则需要通过转到此页面 manage permissions 来撤销客户端。 。您必须寻找您的特定客户。完成此操作后,您可以重新运行您的应用并再次请求权限。
另一个选择是再次创建新的 clientId 和 clientSecret 并确保您拥有正确的范围。我希望这会有所帮助。
关于c# - YouTube 数据 API v.3.0 无法从播放列表添加/删除视频 - InsufficientPermissions 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23500797/