【问题标题】:how to call client_secrets.json in youtube api?如何在 youtube api 中调用 client_secrets.json?
【发布时间】:2018-05-02 16:05:27
【问题描述】:

我正在尝试在我的动态网络应用项目中通过 youtube API 获取 youtube 视频 cmets。

我的代码:

private static int counter = 0;
private static YouTube youtube;

public static void getYoutubeOauth() throws Exception {
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");

    Credential credential = Auth.authorize(scopes, "commentthreads");
    youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).build();

    String videoId = "KIgxmV9xXBQ";

    // Get video comments threads
    CommentThreadListResponse commentsPage = prepareListRequest(videoId).execute();

    while (true) {
        handleCommentsThreads(commentsPage.getItems());

        String nextPageToken = commentsPage.getNextPageToken();
        if (nextPageToken == null)
            break;

        // Get next page of video comments threads
        commentsPage = prepareListRequest(videoId).setPageToken(nextPageToken).execute();
    }

    System.out.println("Total: " + counter);
}

private static YouTube.CommentThreads.List prepareListRequest(String videoId) throws Exception {

    return youtube.commentThreads()
                  .list("snippet,replies")
                  .setVideoId(videoId)
                  .setMaxResults(100L)
                  .setModerationStatus("published")
                  .setTextFormat("plainText");
}

private static void handleCommentsThreads(List<CommentThread> commentThreads) {

    for (CommentThread commentThread : commentThreads) {
        List<Comment> comments = Lists.newArrayList();
        comments.add(commentThread.getSnippet().getTopLevelComment());

        CommentThreadReplies replies = commentThread.getReplies();
        if (replies != null)
            comments.addAll(replies.getComments());

        System.out.println("Found " + comments.size() + " comments.");

        // Do your comments logic here
        counter += comments.size();
    }
}

在执行上述代码时,我在Credential credential = Auth.authorize(scopes, "commentthreads"); 行得到一个java.lang.NullPointerException

仅供参考:我知道NullPointerException 是什么。即string n = null; if(n=="h"); 导致NullPointerException

我希望client_secrets.json 文件可以反驳原因,因为我不知道在哪里放置以及如何在我的代码中调用它。

我当前的client_secrets.json放在/home/UserName/workspace/RemoteSystemsTempFiles/Duck/WebContent/WEB-INF/client_secrets.json

【问题讨论】:

  • 可能范围为空。你试过保护这个变量吗?
  • @TalAvissar“保护这个变量”?你是什​​么意思&请阅读下面的答案评论

标签: java json youtube-api youtube-data-api


【解决方案1】:

您可以按如下方式加载客户端机密 json 文件:

// Load client secrets.
Reader clientSecretReader = new InputStreamReader(Auth.class.getResourceAsStream("/client_secrets.json"));
GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, clientSecretReader);

有关如何使用 Auth 和 google API 的更多信息,请查看here

【讨论】:

  • 感谢您的回复,我正在使用动态网络应用程序,我正在尝试在 servlet 上实现他,所以,在我的情况下,我想我无法将外部 .json 文件添加到我的项目中@ 987654323@,你能告诉我从外部目录读取client_secrets.json吗?我尝试添加Auth.class.getResourceAsStream("/home/hazrat/Documents/eclipse-jee-neon-3-linux-gtk-x86_64/eclipse/client_secrets.json"),但它说空指针异常,尽管我可以通过我的终端访问这个确切的位置。如果你不介意请解释一下list&lt;String&gt; scope 是什么以及放什么?
猜你喜欢
  • 2017-09-08
  • 2023-04-03
  • 1970-01-01
  • 2021-03-02
  • 2015-09-03
  • 2018-09-12
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多