Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The most common way to do it is using the Youtube data API, which will return XML/Json that you can parse to retrieve things like the video url.</p> <p><strong>Updated (2017/01/24) (v3)</strong></p> <p>Use the following call to search for YouTube videos using a search query:</p> <pre><code>https://www.googleapis.com/youtube/v3/search?part=snippet&amp;q=fun%20video&amp;key=YOUR-API-KEY </code></pre> <p>It supports the following basic parameters for searching:</p> <ul> <li><strong>part</strong> : Video data you want to retrieve in the search. For basic searches the recommended value is <strong>snippet</strong>.</li> <li><strong>q</strong> : The text you want to search for</li> <li><strong>key</strong> : Your Google Developer API Key. This key can be obtained at the <a href="https://console.developers.google.com/" rel="nofollow noreferrer">Google Developer API Console</a> on the Credentials page of your application. Make sure to enable the Youtube Data API v3 on the application your key belongs to.</li> </ul> <p>For more parameters see the <a href="https://developers.google.com/youtube/v3/docs/search/list" rel="nofollow noreferrer">Google API Documentation</a></p> <p><strong>Using the Java library</strong></p> <p>On Android you can either do a HTTP request to the URL using the standard HTTP request classes available on the platform, or you can use the <a href="https://developers.google.com/api-client-library/java/" rel="nofollow noreferrer">Google API Java Library</a> as shown below:</p> <pre><code> YouTube youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { public void initialize(HttpRequest request) throws IOException { } }).setApplicationName("YOUR-APPLICATION-NAME").build(); String queryTerm = "A fun video" // Define the API request for retrieving search results. YouTube.Search.List search = youtube.search().list("id,snippet"); search.setKey("Your-Api-Key"); search.setQ(queryTerm); // Call the API and print first result. SearchListResponse searchResponse = search.execute(); if(searchResponse.getItems().size() == 0) { //No items found. return; } SearchResult firstItem = searchResponse.getItems().get(0); ResourceId rId = firstItem.getId(); // Confirm that the result represents a video. Otherwise, the // item will not contain a video ID. if (rId.getKind().equals("youtube#video")) { Thumbnail thumbnail = firstItem.getSnippet().getThumbnails().getDefault(); Log.d("YOUTUBE_SAMPLE","Video Id" + rId.getVideoId()); Log.d("YOUTUBE_SAMPLE","Title: " + firstItem.getSnippet().getTitle()); Log.d("YOUTUBE_SAMPLE","Thumbnail: " + thumbnail.getUrl()); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload