YouTube.NET API-创建聊天消息时的权限问题



我正在Winforms和C#中为YouTube开发我自己的聊天机器人。它已经在Twitch上运行了,我正在尝试使用C#API复制Youtube的功能。我可以下载聊天消息,没有问题,但创建聊天消息让我头疼,因为我收到了403,权限不足的错误。完整的错误消息是

Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]

经过一番搜索,我已经尝试了我能找到的大多数东西,但对于到底是什么原因造成的,我仍然一无所获。我知道这是一个许可问题,我显然需要设置一些东西,但我不知道是什么。我的代码在下面,绝对适用于读取数据。。。但我不知道为什么它对写作不起作用。

  public class YouTubeDataWrapper
    {
        private YouTubeService youTubeService;
        private string liveChatId;
        private bool updatingChat;
        private int prevResultCount;
        public List<YouTubeMessage> Messages { get; private set; }
        public bool Connected { get; private set; }
        public bool ChatUpdated { get; set; }
        //public Authorisation Authorisation { get; set; }
        //public AccessToken AccessToken { get; set; }
        public YouTubeDataWrapper()
        {
            this.Messages = new List<YouTubeMessage>();
        }
        public async void Connect()
        {
            Stream stream = new FileStream("client_secrets.json", FileMode.Open);
            UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, new[] { YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None, new FileDataStore(this.GetType().ToString()));
            stream.Close();
            stream.Dispose();
            this.youTubeService = new YouTubeService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = this.GetType().ToString()
            });
            var res = this.youTubeService.LiveBroadcasts.List("id,snippet,contentDetails,status");
            res.BroadcastType = LiveBroadcastsResource.ListRequest.BroadcastTypeEnum.Persistent;
            res.Mine = true;
            //res.BroadcastStatus = LiveBroadcastsResource.ListRequest.BroadcastStatusEnum.Active;
            var resListResponse = await res.ExecuteAsync();
            IEnumerator<LiveBroadcast> ie = resListResponse.Items.GetEnumerator();
            while (ie.MoveNext() && string.IsNullOrEmpty(this.liveChatId))
            {
                LiveBroadcast livebroadcast = ie.Current;
                string id = livebroadcast.Snippet.LiveChatId;
                if (!string.IsNullOrEmpty(id))
                {
                    this.liveChatId = id;
                    this.Connected = true;
                }
                bool? def = livebroadcast.Snippet.IsDefaultBroadcast;
                string title = livebroadcast.Snippet.Title;
                LiveBroadcastStatus status = livebroadcast.Status;
            }
        }
        public async void UpdateChat()
        {
            if (!this.updatingChat)
            {
                if (!string.IsNullOrEmpty(this.liveChatId) && this.Connected)
                {
                    this.updatingChat = true;
                    var livechat = this.youTubeService.LiveChatMessages.List(this.liveChatId, "id,snippet,authorDetails");
                    var livechatResponse = await livechat.ExecuteAsync();
                    PageInfo pageInfo = livechatResponse.PageInfo;
                    this.ChatUpdated = false;
                    if (pageInfo.TotalResults.HasValue)
                    {
                        if (!this.prevResultCount.Equals(pageInfo.TotalResults.Value))
                        {
                            this.prevResultCount = pageInfo.TotalResults.Value;
                            this.ChatUpdated = true;
                        }
                    }
                    if (this.ChatUpdated)
                    {
                        this.Messages = new List<YouTubeMessage>();
                        foreach (var livemessage in livechatResponse.Items)
                        {
                            string id = livemessage.Id;
                            string displayName = livemessage.AuthorDetails.DisplayName;
                            string message = livemessage.Snippet.DisplayMessage;
                            YouTubeMessage msg = new YouTubeMessage(id, displayName, message);
                            string line = string.Format("{0}: {1}", displayName, message);
                            if (!this.Messages.Contains(msg))
                            {
                                this.Messages.Add(msg);
                            }
                        }
                    }
                    this.updatingChat = false;
                }
            }
        }
        public async void SendMessage(string message)
        {
            LiveChatMessage liveMessage = new LiveChatMessage();
            liveMessage.Snippet = new LiveChatMessageSnippet() { LiveChatId = this.liveChatId, Type = "textMessageEvent", TextMessageDetails = new LiveChatTextMessageDetails() { MessageText = message } };
            var insert = this.youTubeService.LiveChatMessages.Insert(liveMessage, "snippet");
            var response = await insert.ExecuteAsync();
            if (response != null)
            {
            }
        }

}

有问题的主要代码是SendMessage方法。我已经尝试将UserCredentials的作用域更改为我能尝试的所有内容,但没有成功。有什么想法吗?

从YouTube Data API-Error,为请求提供的OAuth 2.0令牌中的error 403 or insufficientPermissions is Error指定的作用域不足以访问请求的数据。

因此,请确保在应用程序中使用正确的Scope。以下是应用程序中所需范围的示例。

https://www.googleapis.com/auth/youtube.force-ssl
https://www.googleapis.com/auth/youtube

有关此错误403的更多信息,您可以查看此相关SO问题。

事实证明,撤消访问然后重新执行可以解决问题。错误消息帮助不大。

相关内容

  • 没有找到相关文章

最新更新