gpt4 book ai didi

c# - YouTube API服务帐户在插入播放列表项时返回 'forbidden 403'

转载 作者:行者123 更新时间:2023-12-03 05:57:41 25 4
gpt4 key购买 nike

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

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
});

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

Error: Google.Apis.Requests.RequestError Access forbidden. The request may not be properly authorized. [403]

Errors [ Message[Access forbidden. The request may not be properly authorized.] Location[ - ] Reason[forbidden] Domain[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是 channel 上传播放列表中的现有项目。我只是复制值并更改播放列表ID。

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

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

最佳答案

引用官方的Google Youtube API文档:

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

refer

关于c# - YouTube API服务帐户在插入播放列表项时返回 'forbidden 403',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31653635/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com