【问题标题】:How to get URL of a video using youtube API v3 using Java如何使用 youtube API v3 使用 Java 获取视频的 URL
【发布时间】:2014-04-08 04:22:41
【问题描述】:

我正在使用 Youtube API v3 按关键字搜索视频。我目前正在使用此代码:

    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 示例,我很迷茫。

有什么帮助吗?

谢谢

【问题讨论】:

    标签: list url search video youtube


    【解决方案1】:

    我知道这是一个老话题,但对于从 Googleland 来到这里的其他人来说,这里有一个答案。

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

    只需将 rId.getVideoId() 添加到 youtube.com/watch?v= 即可获得网址。

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

    【讨论】:

    • @Have No Display Name:感谢您的编辑。在平板电脑上很难使用代码功能。 :)
    • @Mark:- 它发生了:-)
    猜你喜欢
    • 2015-10-12
    • 2015-12-01
    • 2016-01-21
    • 2014-06-05
    • 2015-08-29
    • 2018-06-28
    • 2015-11-23
    • 2019-01-28
    • 2013-10-14
    相关资源
    最近更新 更多