【问题标题】:YouTube API service account returns 'forbidden 403' when inserting playlist itemYouTube API 服务帐户在插入播放列表项时返回“禁止 403”
【发布时间】:2015-10-17 16:05:46
【问题描述】:

我正在编写一个简单的应用程序,它从订阅的频道获取一些新的上传内容并将它们添加到指定的播放列表中。服务帐号类型与以下代码一起使用:

var certificate = new X509Certificate2(certificateFileName, "notasecret", X509KeyStorageFlags.Exportable);
var serviceInitializer = new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
        Scopes = new[] { YouTubeService.Scope.Youtube }
};
var serviceInitializerInstance = serviceInitializer.FromCertificate(certificate);

var credential = new ServiceAccountCredential(serviceInitializerInstance);
var youtubeService = new YouTubeService(new BaseClientService.Initializer
{
        HttpClientInitializer = credential,
        ApplicationName = applicationName
});

我已成功读取所有必要信息,但当我尝试将项目插入播放列表时,它总是返回此错误:

错误:Google.Apis.Requests.RequestError 禁止访问。请求 可能未得到适当授权。 [403]

错误 [ 消息[禁止访问。该请求可能未得到适当授权。] 位置[ - ] 原因[禁止] 域[yo utube.common] ]

根据this链接,这似乎是一些Core API一般错误。

我看过很多样本,它们都一样。我的代码是:

var newPlaylistItemClone = new PlaylistItem();
newPlaylistItem.Snippet = new PlaylistItemSnippet();
newPlaylistItem.Snippet.PlaylistId = playlist.Id;
newPlaylistItem.Snippet.ResourceId = new ResourceId();
newPlaylistItem.Snippet.ResourceId.VideoId = existingPlaylistItem.Snippet.ResourceId.VideoId;
newPlaylistItem.Snippet.ResourceId.Kind = existingPlaylistItem.Snippet.ResourceId.Kind;

await youTubeService.PlaylistItems.Insert(newPlaylistItem, "snippet").ExecuteAsync();

newPlaylistItem 是我要插入的项目,existingPlaylistItem 是频道上传播放列表中的现有项目。我只是复制值并更改播放列表 ID。

此外,在 Google Developers 文档网站上使用 API Explorer 时,一切正常(使用 Web 授权)。在 Google Developers Console 中,我启用了所有 API。也许问题是我缺少服务帐户类型所需的一些信息? 谁能帮我解决这个问题?

这是我的GitHub 带有源代码的存储库。应请求在第 235 行发生错误。

【问题讨论】:

    标签: c# .net api youtube youtube-api


    【解决方案1】:

    最简单的方法是转到您的应用程序的同意屏幕。

    通过添加 YouTube 范围编辑您的应用(您可以根据需要添加更多范围)。重新登录并再次测试播放列表功能

    为我工作,我设法通过 YouTubeService 类获取帐户的播放列表

    【讨论】:

      【解决方案2】:

      引用自 Google Youtube API 官方文档:

      服务帐户不适用于 YouTube 数据 API 调用,因为服务帐户需要关联的 YouTube 频道,并且您无法将新频道或现有频道与服务帐户相关联。如果您使用服务帐户调用 YouTube 数据 API,API 服务器会返回一个错误,错误类型设置为未授权,原因设置为 youtubeSignupRequired。

      请refer。

      【讨论】:

      • 谢谢!。我一直在寻找很多文档,但错过了这个。
      猜你喜欢
      • 1970-01-01
      • 2021-12-21
      • 2013-09-20
      • 2022-11-07
      • 2015-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      相关资源
      最近更新 更多