Insufficient Permission when run youtube retriving comments

前端 未结 4 2010
离开以前
离开以前 2020-12-20 03:40

This is my whole code. I want to give a video ID which from youtube to get the comments related to this vedio ID. But always show that I have Insufficient Permission.

4条回答
  •  情书的邮戳
    2020-12-20 04:26

    As the error message indicates, your request does not have sufficient permissions. If you look at the API Reference Overview you will see:

    Every request must either specify an API key (with the key parameter) or provide an OAuth 2.0 token. Your API key is available in the API console's API Access pane for your project.
    

    For example I am able to view the comment thread list for a video by making a GET request to this link in the browser directly: https://www.googleapis.com/youtube/v3/commentThreads?part=snippet&key=YOUR_KEY&videoId=tLTm_POao1c. You will need to replace YOUR_KEY with your application key that you can find in your Google developer console.

    I don't know why the code sample for comment threads does not mention anything about the need to include the API key (probably because it is assumed that you read the API Overview first). But if you look at this other code sample, you will see that you can include a developer key in a local file that you can load into a Properties object:

        // Read the developer key from the properties file.
        Properties properties = new Properties();
        try {
            InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
            properties.load(in);
    
        } catch (IOException e) {
            System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
                    + " : " + e.getMessage());
            System.exit(1);
        }
    

    Further down the line, the api key is extracted from the Properties object and is used in the search API call:

            // Set your developer key from the Google Developers Console for
            // non-authenticated requests. See:
            // https://console.developers.google.com/
            String apiKey = properties.getProperty("youtube.apikey");
            search.setKey(apiKey);
            search.setQ(queryTerm);
    

    In a similar manner, you can call setKey() on your code, as described by the JavaDocs: https://developers.google.com/resources/api-libraries/documentation/youtube/v3/java/latest/com/google/api/services/youtube/YouTube.CommentThreads.List.html#setKey(java.lang.String)

    So, you may need to add something like this:

     CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
     .list("snippet")
     .setKey(YOUR_KEY)
     .setVideoId("tLTm_POao1c")
     .setTextFormat("plainText")
     .execute();
    

    You don't even need the Properties file, unless you plan to change the API key after you write the code and deploy it.

提交回复
热议问题