在尝试使用youtube DATA API进行操作时遇到此错误,但标题上列出了带有标签[207:0]的错误。我检查了花括号,它们似乎相互对应。可能还有什么其他问题?
在这段代码中,我试图提取视频信息,例如标题,作者,描述和观看次数。
public class Search {
/** Global instance properties filename. */
private static String PROPERTIES_FILENAME = "youtube.properties";
/** Global instance of the HTTP transport. */
private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
/** Global instance of the JSON factory. */
private static final JsonFactory JSON_FACTORY = new JacksonFactory();
/** Global instance of the max number of videos we want returned (50 = upper limit per page). */
private static final long NUMBER_OF_VIDEOS_RETURNED = 25;
/** Global instance of Youtube object to make all API requests. */
private static YouTube youtube;
/**
* Initializes YouTube object to search for videos on YouTube (Youtube.Search.List). The program
* then prints the names and thumbnails of each of the videos (only first 50 videos).
*
* @param args command line args.
*/
public static void main(String[] args) {
// Read the developer key from youtube.properties
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);
}
try {
/*
* The YouTube object is used to make all API requests. The last argument is required, but
* because we don't need anything initialized when the HttpRequest is initialized, we override
* the interface and provide a no-op function.
*/
youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {}
}).setApplicationName("youtube-cmdline-search-sample").build();
// Get query term from user.
String queryTerm = getInputQuery();
YouTube.Search.List search = youtube.search().list("id,snippet");
/*
* It is important to set your developer key from the Google Developer Console for
* non-authenticated requests (found under the API Access tab at this link:
* code.google.com/apis/). This is good practice and increased your quota.
*/
String apiKey = properties.getProperty("youtube.apikey");
search.setKey(apiKey);
search.setQ(queryTerm);
/*
* We are only searching for videos (not playlists or channels). If we were searching for
* more, we would add them as a string like this: "video,playlist,channel".
*/
search.setType("video");
/*
* This method reduces the info returned to only the fields we need and makes calls more
* efficient.
*/
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/description,snippet/channelId,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
SearchListResponse searchResponse = search.execute();
List<SearchResult> searchResultList = searchResponse.getItems();
List<String> videoIds = new ArrayList<String>();
if (searchResultList != null) {
prettyPrint(videoList.iterator(),queryTerm);
// Merge video IDs
for (SearchResult searchResult : searchResultList) {
videoIds.add(searchResult.getId().getVideoId());
}
Joiner stringJoiner = Joiner.on(',');
String videoId = stringJoiner.join(videoIds);
// Call the YouTube Data API's youtube.videos.list method to
// retrieve the resources that represent the specified videos.
YouTube.Videos.List listVideosRequest = youtube.videos().list("snippet, recordingDetails").setId(videoId);
VideoListResponse listResponse = listVideosRequest.execute();
List<Video> videoList = listResponse.getItems();
if (videoList != null) {
prettyPrint(videoList.iterator(), queryTerm);
}
}
} catch (GoogleJsonResponseException e) {
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (IOException e) {
System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
}
/*
* Returns a query term (String) from user via the terminal.
*/
private static String getInputQuery() throws IOException {
String inputQuery = "";
System.out.print("Please enter a search term: ");
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
inputQuery = bReader.readLine();
if (inputQuery.length() < 1) {
// If nothing is entered, defaults to "YouTube Developers Live."
inputQuery = "YouTube Developers Live";
}
return inputQuery;
}
/*
* Prints out all SearchResults in the Iterator. Each printed line includes title, id, and
* thumbnail.
*
* @param iteratorSearchResults Iterator of SearchResults to print
*
* @param query Search query (String)
*/
private static void prettyPrint(Iterator<Video> iteratorVideoResults,String query) {
System.out.println("\n=============================================================");
System.out.println(
" First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
System.out.println("=============================================================\n");
if (!iteratorVideoResults.hasNext()) {
System.out.println(" There aren't any results for your query.");
}
while (iteratorVideoResults.hasNext()) {
Video singleVideo = iteratorVideoResults.next();
Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().getDefault();
System.out.println(" Video Id: " + singleVideo.getId());
System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
System.out.println(" Description: " + singleVideo.getSnippet().getDescription());
System.out.println(" Author: " + singleVideo.getSnippet().getChannelId());
System.out.println(" Thumbnail: " + thumbnail.getUrl());
System.out.println("View count: "+singleVideo.getStatistics().getViewCount());
System.out.println("\n-------------------------------------------------------------\n");
}
}
}