Sunday, 21 June 2015

YouTube Comments and Commenting with YouTube API V3 (Google.Apis.YouTube.v3)

Google deprecated V2 of the YouTube API over a year ago, but as of last month, the URLs have been taken offline; forcing users to upgrade to V3 of the API. One of the last features requiring implementating on the V3 platform was the ability to comment on channels and videos. The following example shows how to leave a top level comment on a channel or video using V3 of the API with C#

Code Snippet
  2. YouTubeService youtube; // Init your service here - Make sure you include the new required scope: YouTubeService.Scope.YoutubeForceSsl
  4. CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
  5. //commentThreadSnippet.ChannelId; Comment on a channel - leave video id blank if so
  6. commentThreadSnippet.VideoId = v.ID; // Comment on a Video by VideoID
  8. // A top level comment is a new comment (Not a reply to an existing comment)
  9. commentThreadSnippet.TopLevelComment = new Comment() { Snippet = new CommentSnippet() { TextOriginal = comment }};
  11. // Make an insert request and get result snippet
  12. CommentThreadsResource.InsertRequest commentReq = youtube.CommentThreads.Insert(new CommentThread() { Snippet = commentThreadSnippet}, "snippet");
  13. CommentThread commentRes = commentReq.Execute();
End of Code Snippet