【问题标题】:C# Adding Comments with YouTube V3 API [duplicate]C# 使用 YouTube V3 API 添加评论 [重复]
【发布时间】:2016-04-06 18:59:54
【问题描述】:

我正在使用 C# 并使用 YouTube V3 API。我正在尝试在视频中插入评论,但是每当我这样做时,我都会收到 "{"Object reference not set to an instance of an object."}" 的异常,每当我运行类似于上述代码的任何内容时都会发生这种情况:

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();
}

我将此与谷歌资源管理器进行比较并使用相同的属性,并且资源管理器实际上在我的程序失败的地方添加了 cmets。 https://developers.google.com/youtube/v3/docs/commentThreads/insert

一到代码的第二行 commentToAdd.Snippet.IsPublic = true;

上面的每一行都会出错并继续。

任何帮助将不胜感激。

【问题讨论】:

  • 您正在使用 Snippet 对象上的设置器。你确定Snippet 不是null

标签: c# youtube-api


【解决方案1】:

您的问题在于Snippetnull

取自您提供的API link,您需要先创建一个CommentSnippet

在 Google 提供的示例中:

// 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

【讨论】:

  • 请查看其他评论。非常感谢您的帮助。
【解决方案2】:

非常感谢您的帮助。设法完成它。

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();
    }

【讨论】:

  • 您基本上只是将 Nikola 的答案包装在 async / await 中。至少接受他的回答并删除你的另一个“回答”。
猜你喜欢
  • 2017-06-07
  • 2018-01-26
  • 2015-08-23
  • 1970-01-01
  • 1970-01-01
  • 2018-03-19
  • 1970-01-01
  • 2021-02-27
  • 1970-01-01
相关资源
最近更新 更多