C#使用YouTube V3 API添加评论
我正在使用C#和YouTube V3 API。我试图在视频中插入注释,但每当我插入注释时,我会收到一个异常,即C#使用YouTube V3 API添加评论,c#,youtube-api,C#,Youtube Api,我正在使用C#和YouTube V3 API。我试图在视频中插入注释,但每当我插入注释时,我会收到一个异常,即“{”对象引用未设置为对象的实例。“}”每当我运行类似于上述代码的任何操作时,都会发生这种情况: public void AddComment() { CommentThread commentToAdd = new CommentThread(); commentToAdd.Snippet.IsPublic = true; commentToAdd.Snippe
“{”对象引用未设置为对象的实例。“}”
每当我运行类似于上述代码的任何操作时,都会发生这种情况:
public void AddComment()
{
CommentThread commentToAdd = new CommentThread();
commentToAdd.Snippet.IsPublic = true;
commentToAdd.Snippet.TopLevelComment.Snippet.TextOriginal = "Test";
commentToAdd.Snippet.VideoId = "kc-LBxBcyG8";
commentToAdd.Snippet.TopLevelComment.Snippet.VideoId = "kc-LBxBcyG8";
CommentThreadsResource.InsertRequest ins = JKYouTube.NewYouTubeService().CommentThreads.Insert(commentToAdd, "snippet");
var insertedComment = ins.Execute();
}
我将其与谷歌浏览器进行比较,并使用相同的属性,浏览器实际上会添加注释,因为我的程序刚刚失败
当它到达第二行代码时
commentoadd.Snippet.IsPublic=true代码>
它只会出错并继续上面的每一行
任何帮助都将不胜感激 您的问题在于代码段
为空
根据您给出的注释,您需要首先创建一个CommentSnippet
在谷歌提供的示例中:
// Insert channel comment by omitting videoId.
// Create a comment snippet with text.
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.setTextOriginal(text);
首先,使用一些文本创建一个CommentSnippet
,然后我们创建一个顶级注释:
// Create a top-level comment with snippet.
Comment topLevelComment = new Comment();
topLevelComment.setSnippet(commentSnippet);
然后,将您的topLevelComment
添加到CommentThreadSnippet
:
// Create a comment thread snippet with channelId and top-level
// comment.
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.setChannelId(channelId);
commentThreadSnippet.setTopLevelComment(topLevelComment);
当您最终拥有CommentThreadSnippet
时,您可以将其添加到CommentThread
:
// Create a comment thread with snippet.
CommentThread commentThread = new CommentThread();
commentThread.setSnippet(commentThreadSnippet);
遵循这些步骤不应给您带来NRE非常感谢您的帮助。我设法完成了它
async Task AddVideoCommentAsync(string commentToAdd, string videoID)
{
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.TextOriginal = commentToAdd;
Comment topLevelComment = new Comment();
topLevelComment.Snippet = commentSnippet;
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.VideoId = videoID;
commentThreadSnippet.TopLevelComment = topLevelComment;
CommentThread commentThread = new CommentThread();
commentThread.Snippet = commentThreadSnippet;
var youtubeService = await NewYouTubeService();
CommentThreadsResource.InsertRequest insertComment = youtubeService.CommentThreads.Insert(commentThread, "snippet");
await insertComment.ExecuteAsync();
}
您正在代码段
对象上使用setter。您确定代码段
不是null
?请参阅其他注释。非常感谢您的帮助。您基本上只是将Nikola的答案包装为async/await。至少接受他的答案,删除你的另一个“答案”。