2016-03-09 103 views
3

我越来越低于错误,我想让所有评论发布在YouTube视频上。Google.Apis.Requests.RequestError权限不足[403]

所以基本上我传递视频ID,我想获得与视频

Google.Apis.Requests.RequestError
没有足够的许可[403]

错误[消息相关联的所有评论[没有足够的权限]地址[ - ]原因[insufficientPermissions]域[全球]

这里是我的代码:

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

回答

0

我知道这个答案有点晚,但这是为我修复的。希望它可以帮助别人。

我正在收到相同的权限被拒绝的错误。在将YouTubeService.Scope.YoutubeForceSsl项目添加到范围列表后,我能够提出评论。

此外,它看起来像你的代码是不正确的。您将需要根据VideoId拉取CommentThreads并包含回复(如果您需要的话)。

您将无法使用评论为视频提出评论。

var threadsRequest = Client.CommentThreads.List("snippet,replies"); 

threadsRequest.VideoId = videoId; 

var response = threadsRequest.Execute(); 
2

这是一个真的反应迟缓,但我也有类似的问题。我的项目使用YouTube API将视频上传到帐户,并在代码的另一部分中再次使用它来通过ID搜索视频以检查其状态。

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

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

我对这个简单的解决方案是创建通过谷歌开发者控制台的另一组OAuth认证的(类型为“其他”中),下载JSON文件,并使用这些信息来获得不同的访问令牌的搜索部分我码。

希望这可以帮助别人。

0

在授权更改“用户”到“管理”。

+0

tbh..This应该是一个评论...但缺乏代表... – ZF007