我收到以下错误,我想在 YouTube 视频上发布所有评论。

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



这是我的代码:

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

最佳答案

GoogleWebAuthorizationBroker.AuthorizeAsync 中,将“ 用户 ”更改为“ admin ”。

关于c# - Google.Apis.Requests.RequestError 权限不足 [403],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35880845/

10-13 08:23