【发布时间】:2015-10-11 00:41:09
【问题描述】:
我正在尝试使用一个简单的 C# 应用删除一个或多个视频(我打算稍后使用 Windows 服务),但我收到了这个错误:
Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""
at Google.Apis.Requests.ClientServiceRequest`1.Execute() in c:\code\google.com\google-api-dotnet-client\default\Tools\Google.Apis.Release\bin\Debug\test\default\Src\GoogleApis\Apis\Requests\ClientServiceRequest.cs:line 93
上传视频效果很好。对于这两个操作,我使用相同的初始化方法:
private static YouTubeService AuthorizeYoutubeService()
{
string serviceAccountEmail = "...@developer.gserviceaccount.com";
string keyFilePath = "Warehouse<...>.p12";
string userAccountEmail = "login@gmail.com";
if (!File.Exists(keyFilePath))
{
System.Windows.Forms.MessageBox.Show("Secret file not found!");
return null;
}
var scope = new string[] { YouTubeService.Scope.Youtube };
var cert = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable);
try
{
ServiceAccountCredential credential = new ServiceAccountCredential
(new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = scope,
User = userAccountEmail
}.FromCertificate(cert));
var service = new YouTubeService(new BaseClientService.Initializer
{
HttpClientInitializer = credential,
ApplicationName = "warehouse"
});
return service;
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
return null;
}
}
与简单上传视频的区别在于定义的范围:YouTubeService.Scope.YoutubeUpload。当我尝试使用它删除视频时,我收到 insufficientPermissions (403) 错误。 因此,在查看了documentation 之后,我将其更改为 YouTubeService.Scope.Youtube。
这是我尝试使用的代码:
var youtubeService = AuthorizeYoutubeService();
foreach (string id in deleteIds)
{
var videoDeleteRequest = youtubeService.Videos.Delete(id);
var result = videoDeleteRequest.Execute();
}
其中 deleteIds 是包含现有视频 ID 的 11 个字符串的列表。 我在开发者控制台中启用了 YouTube 数据 API。 我已经通过 NuGet 安装了 API,我认为这些包没有任何问题。
我对 Google 开发还很陌生,所有类似的问题都是关于日历 API 的。
感谢您的帮助。
【问题讨论】:
标签: c# .net youtube youtube-data-api