Reputation: 841
I am using the youtube api-samples java code to list video by ID. I used the Topics.java file but it gives more than 1 result which includes the correct one. Also it does not apply the video ID (when I printed it gives the video ID as 0) and so I gave the video ID for the search term as well. Below is the screenshot of the inputs and output.
What I am trying to achieve is getting exact one result for the given video ID like how it works on the YouTube Data API code snippet.
Am I running the wrong code or am I giving wrong inputs? Please advise.
Update: File VideoLocalizations.java throws null pointer exception for "language"
Upvotes: 0
Views: 3849
Reputation: 673
The Topics.java
file is demo-ing the Topics API, which I think is more than you want to do.
To simply get the details for a video ID, check out Videos.list
. You can use the API Explorer at the bottom of the page to create a basic request, e.g.
GET https://www.googleapis.com/youtube/v3/videos?
part=id%2Csnippet&id=5vY8EWokf40&key={YOUR_API_KEY}
...which returns:
{
"kind": "youtube#videoListResponse",
"etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/v-nx5E3jmblZ7cA3yNCjAAKmywQ\"",
"pageInfo": {
"totalResults": 1,
"resultsPerPage": 1
},
"items": [
{
"kind": "youtube#video",
"etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/rsLwePwmFcmdkMVDPrQB20sLp1Q\"",
"id": "5vY8EWokf40",
"snippet": {
"publishedAt": "2015-06-27T03:02:46.000Z",
"channelId": "UCz0Am9KlCDydaIifIL16hfw",
"title": "Hajar Film - Sirin Hamsho | فيلم هاجر - سيرين حمشو",
...
To do this in Java, look at the Examples section on the same page. Essentially, you'll need to create a com.google.api.services.youtube.YouTube
object, call videos().list()
, and handle the VideoListResponse
.
Looking at the JAVA #2 example, you can see this demonstrated as:
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
.setApplicationName("youtube-cmdline-localizations-sample").build();
...
// Call the YouTube Data API's videos.list method to retrieve videos.
VideoListResponse videoListResponse = youtube.videos().
list("snippet,localizations").setId(videoId).execute();
// Since the API request specified a unique video ID, the API
// response should return exactly one video. If the response does
// not contain a video, then the specified video ID was not found.
List<Video> videoList = videoListResponse.getItems();
if (videoList.isEmpty()) {
System.out.println("Can't find a video with ID: " + videoId);
return;
}
Video video = videoList.get(0);
You can look at the JavaDoc reference for the YouTube Data API for more details on using the Java library.
Upvotes: 2