【问题标题】:YouTube Data API v3: deleting videos using a service account: Unauthorized client or scope in requestYouTube Data API v3:使用服务帐户删除视频:未经授权的客户端或请求范围
【发布时间】: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


    【解决方案1】:

    我最终做的是重置连接到 Google 帐户的应用列表并从头开始重新设置。由于某种原因,我的应用被添加了 2 次。

    【讨论】:

      猜你喜欢
      • 2015-07-06
      • 2017-02-22
      • 2015-02-01
      • 1970-01-01
      • 2016-10-13
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2022-09-19
      相关资源
      最近更新 更多