【问题标题】:Google.Apis.Requests.RequestError Insufficient Permission [403]Google.Apis.Requests.RequestError 权限不足 [403]
【发布时间】:2026-02-16 12:45:02
【问题描述】:

我遇到了错误,我想将所有 cmets 发布到 youtube 视频上。

所以基本上我正在传递视频 ID,我想获取与该视频相关的所有 cmets

Google.Apis.Requests.RequestError
权限不足 [403]

错误[Message[Insufficient Permission] Location[-] Reason[insufficientPermissions] Domain[global]]

这是我的代码:

protected void btnGetVideoDesc_Click(object sender, EventArgs e)
{
    string videoId = txtVideoID.Text;
    YoutubeVideo video = new YoutubeVideo(videoId);
    lblTitle.Text = video.title;
    lblPublishedDate.Text = video.publishdate.ToShortDateString();
}

public class YoutubeVideo
{
    public string id, title, description ;
    public DateTime publishdate;

    public YoutubeVideo(string id)
    {
        this.id = id;
        YoutubeAPI.GetVideoInfo(this);
    }
}

public class YoutubeAPI
{
    private static YouTubeService ytService = Auth();

    private static YouTubeService Auth()
    {
        UserCredential creds;
        var service = new YouTubeService();
        try
        {
            using (var stream = new FileStream(@"C:\v-mmarat\Project\EMEA_Development\YoutubeWebCrawling\YoutubeWebCrawling\youtube_client_secret.json", FileMode.Open, FileAccess.Read))
            {
                creds = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets,
                    new[] { YouTubeService.Scope.YoutubeReadonly }, "user", CancellationToken.None,
                    new FileDataStore("YoutubeAPI")
                    ).Result;
            }
           service = new YouTubeService(new BaseClientService.Initializer()
           {
               HttpClientInitializer = creds,
               ApplicationName = "YoutubeAPI",
               ApiKey = "My_API_Key"

           });
        }
        catch (Exception e)
        { }
        return service;
    }

    public static void GetVideoInfo(YoutubeVideo video)
    {
        try
        {
           //This code work perfectly 
            var videoRequest = ytService.Videos.List("snippet");

            videoRequest.Id = video.id;

            var response = videoRequest.Execute();
            if (response.Items.Count > 0)
            {
                video.title = response.Items[0].Snippet.Title;
                video.description = response.Items[0].Snippet.Description;
                video.publishdate = response.Items[0].Snippet.PublishedAt.Value;
            }              




        else
            {
                //error
            }

            var CommentRequest = ytService.Comments.List("snippet");

            videoRequest.Id = video.id;

            //Getting error at this line after CommentRequest.Execute();
            var Commentresponse = CommentRequest.Execute();
            if (Commentresponse.Items.Count > 0)
            {
                video.title = Commentresponse.Items[0].Snippet.ChannelId;
                video.description = Commentresponse.Items[0].Snippet.TextDisplay;
                video.publishdate = Commentresponse.Items[0].Snippet.PublishedAt.Value;
            }
            else
            {
                //error
            }
        }
        catch (Exception e)
        { }
    }

【问题讨论】:

    标签: c# youtube-data-api


    【解决方案1】:

    这里也一样。
    通过在“GoogleWebAuthorizationBroker.AuthorizeAsync”中指定“dataStore”参数解决了这个问题。
    该服务将访问令牌 JSON 文件写入该文件夹。
    似乎授权需要存储选项。
    此外,当授权成功时,浏览器会跳转到一个 Google 授权 URL,我必须登录并允许我从 API 请求的访问级别。

    我之前一直使用 API,但仅用于只读操作。 似乎“动作”的东西(插入、更新、删除、上传)需要更多的授权。

    【讨论】:

      【解决方案2】:

      GoogleWebAuthorizationBroker.AuthorizeAsync 中将“user”更改为“admin”。

      【讨论】:

        【解决方案3】:

        这是一个真的延迟回复,但我遇到了类似的问题。我的项目使用 YouTube API 将视频上传到一个帐户,并且在代码的一个单独部分中再次使用它来按 ID 搜索视频以检查其状态。

        我的问题是我使用相同的 OAuth 凭据进行上传,然后还用于搜索视频。

        这失败了,因为我在上传时已经设置了 YouTube 范围,这不是搜索视频的正确范围。

        对此我的简单解决方案是通过 Google 开发者控制台创建另一组 OAuth 凭据(类型为“其他”),下载 json 文件并使用这些详细信息为我的代码的搜索部分获取不同的访问令牌.

        希望这对某人有所帮助。

        【讨论】:

          【解决方案4】:

          我知道这个答案有点晚了,但这就是为我解决的问题。希望它可以帮助其他人。

          我收到了相同的权限被拒绝错误。将YouTubeService.Scope.YoutubeForceSsl 项添加到范围列表后,我能够拉出 cmets。

          此外,您的代码似乎不太正确。您需要根据 VideoId 提取 CommentThreads 并包含回复(如果需要)。

          您将无法使用评论拉取视频的 cmets。

          var threadsRequest = Client.CommentThreads.List("snippet,replies");
          
          threadsRequest.VideoId = videoId;
          
          var response = threadsRequest.Execute();
          

          【讨论】: