Я пытаюсь удалить одно или несколько видео с помощью простого приложения C# (я намерен использовать Windows Service позже), и я получение этой ошибки:API данных YouTube v3: удаление видео с помощью учетной записи службы: неавторизованный клиент или область в запросе
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 = "[email protected]";
string keyFilePath = "Warehouse<...>.p12";
string userAccountEmail = "[email protected]";
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. Когда я пытаюсь удалить видео, используя его, я получаю ошибку недостаточной ошибки (403). Итак, посмотрев в documentation, я изменил его на YouTubeService.Scope.Youtube.
Вот код, который я пытаюсь использовать:
var youtubeService = AuthorizeYoutubeService();
foreach (string id in deleteIds)
{
var videoDeleteRequest = youtubeService.Videos.Delete(id);
var result = videoDeleteRequest.Execute();
}
Где deleteIds список из 11 символьных строк, содержащих идентификаторы существующих видео. У меня есть API данных YouTube, который включен в консоли разработчиков. Я установил API через NuGet, я не думаю, что с пакетами что-то не так.
Я довольно новичок в разработке Google, и все подобные вопросы касались API-интерфейса календаря.
Я ценю любую помощь.