Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#使用YouTube V3 API添加评论_C#_Youtube Api - Fatal编程技术网

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

我正在使用C#和YouTube V3 API。我试图在视频中插入注释,但每当我插入注释时,我会收到一个异常,即
“{”对象引用未设置为对象的实例。“}”
每当我运行类似于上述代码的任何操作时,都会发生这种情况:

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。至少接受他的答案,删除你的另一个“答案”。