Youtube api how get replies to comments and likes

2019-08-18 01:57发布

问题:

To get comments using this

Comment Threads: list

GET https://www.googleapis.com/youtube/v3/commentThreads?part=snippet

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "dailyLimitExceededUnreg",
    "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.",
    "extendedHelp": "https://code.google.com/apis/console"
   }
  ],
  "code": 403,
  "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
 }
}

But how get each comment replies, and check user like it or not, any knows?

回答1:

You may use the comments.list method in retrieving comment replies. Here's an example:

// Call the YouTube Data API's comments.list method to retrieve
// existing comment
// replies.
               CommentListResponse commentsListResponse = youtube.comments().list("snippet")
                        .setParentId(parentId).setTextFormat("plainText").execute();
                List<Comment> comments = commentsListResponse.getItems();

                if (comments.isEmpty()) {
                    System.out.println("Can't get comment replies.");
                } else {
                    // Print information from the API response.
                    System.out
                            .println("\n================== Returned Comment Replies ==================\n");
                    for (Comment commentReply : comments) {
                        snippet = commentReply.getSnippet();
                        System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                        System.out.println("  - Comment: " + snippet.getTextDisplay());
                        System.out
                                .println("\n-------------------------------------------------------------\n");
                    }
                    Comment firstCommentReply = comments.get(0);
                    firstCommentReply.getSnippet().setTextOriginal("updated");
                    Comment commentUpdateResponse = youtube.comments()
                            .update("snippet", firstCommentReply).execute();
                    // Print information from the API response.
                    System.out
                            .println("\n================== Updated Video Comment ==================\n");
                    snippet = commentUpdateResponse.getSnippet();
                    System.out.println("  - Author: " + snippet.getAuthorDisplayName());
                    System.out.println("  - Comment: " + snippet.getTextDisplay());
                    System.out
                            .println("\n-------------------------------------------------------------\n");

Regarding likes, you may want to check out the snippet.viewerRating.

The rating the viewer has given to this comment. Note that this property does not currently identify dislike ratings, though this behavior is subject to change. In the meantime, the property value is like if the viewer has rated the comment positively. The value is none in all other cases, including the user having given the comment a negative rating or not having rated the comment.

Valid values for this property are:

  • like
  • none

Then check the snippet.likeCount to get the total number of likes (positive ratings) the comment has received.

Here's the sample JSON structure that shows the format of a comments resource.

{
  "kind": "youtube#comment",
  "etag": etag,
  "id": string,
  "snippet": {
    ......
    "authorChannelId": {
      "value": string
    },
    ......
    "viewerRating": string,
    "likeCount": unsigned integer,
    ......
  }
}

Hope this helps!