2014-03-05 196 views
1

我使用Youtube API v3按關鍵字搜索視頻。我目前正在使用此代碼:如何使用youtube API v3使用Java獲取視頻的URL

YouTube youtube; 
youtube = new YouTube.Builder(GoogleNetHttpTransport.newTrustedTransport(), new JacksonFactory(), new HttpRequestInitializer() { 
public void initialize(HttpRequest request) throws IOException { 
}}).setApplicationName("youtube-cmdline-search-sample").build(); 
// query term. 
String keyword = request.getParameter("keyword"); 

// api key 
String apiKey = "my-api-key"; 

// Define the API request for retrieving search results. 
YouTube.Search.List search = youtube.search().list("id,snippet"); 

// Set your developer key from the Google Cloud Console for 
// non-authenticated requests. See: 
// https://cloud.google.com/console 
lstVideos.setKey(apiKey); 
search.setKey(apiKey); 
search.setQ(keyword); 

// To increase efficiency, only retrieve the fields that the 
// application uses. 
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)"); 
search.setMaxResults((long)10); 
// Call the API and print results. 
SearchListResponse searchResponse = search.execute(); 
List<SearchResult> searchResultList = searchResponse.getItems(); 
Iterator<SearchResult> itsearch = searchResultList.iterator(); 

這對我得到一些數據如(id,縮略圖等)的罰款。此代碼可在Youtube v3 API examples上獲得。

我看到上一個問題在stackoverflow(this),基本上是相同的,但..我無法弄清楚如何獲得這些數據。我看不到任何Java示例,我很迷茫。

任何幫助?

感謝

回答

0

我知道這是一個老話題,但對於其他人是這裏的土地從Googleland,這裏有一個答案。

SearchResult singleVideo = iteratorSearchResults.next(); 
ResourceId rId = singleVideo.getId(); 

只是釘rId.getVideoId()到youtube.com/watch?v=和你的網址。

String url = "youtube.com/watch?v=" + rId.getVideoId(); 
+0

@無顯示名稱:感謝您的編輯。在平板電腦上使用代碼功能很困難。 :) –

+0

@Mark: - 它發生:-) – HaveNoDisplayName

相關問題