【问题标题】:YouTube .NET API - Permission issue when creating chat messageYouTube .NET API - 创建聊天消息时的权限问题
【发布时间】:2016-11-24 13:54:05
【问题描述】:

我正在使用 Winforms 和 C# 为 YouTube 开发自己的 ChatBot。它已经在 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)
            {

            }

        }

}

有问题的主要代码是发送消息方法。我尝试将 UserCredentials 的范围更改为我可以尝试的所有内容,但无济于事。有什么想法吗?

【问题讨论】:

  • 您的用户是否使用他们的 GoogleAccount 登录?
  • 应该是,我的意思是它在我第一次运行代码时经历了这个过程(如果这就是你的意思 - 否则我不确定)。
  • “通过过程”?好吧,通常,每次任何人打开您的应用程序(并且尚未登录)时,谷歌都必须向他询问他的身份,并且用户必须向谷歌服务提供用户名和密码。实施了吗?
  • 啊,看到了。抱歉,一开始肯定忽略了。是的,已实现身份验证。
  • 没有问题,至少我做对了!

标签: c# youtube-api google-api-dotnet-client youtube-data-api


【解决方案1】:

YouTube Data API - Error 来看,您的error 403 or insufficientPermissions 是为请求提供的OAuth 2.0 令牌中的错误,指定的范围不足以访问所请求的数据。

所以请确保在您的应用程序中使用正确的Scope。这是您的应用程序所需的范围示例。

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

https://www.googleapis.com/auth/youtube

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

【讨论】:

  • 从代码中可以看出,我使用了正确的范围。我已经尝试过 YouTube 和 YouTubeForceSSL,但都没有工作。也许,如果我能弄清楚如何,我会撤销访问并重做。
  • 好的,撤销并重试修复它...不知道为什么!
【解决方案2】:

事实证明,撤消访问权限然后重新进行访问可以解决问题。错误消息不是很有帮助。

【讨论】:

    猜你喜欢
    • 2018-03-10
    • 1970-01-01
    • 1970-01-01
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多