Retorna um conjunto de resultados de pesquisa que correspondem a parâmetros de consulta especificados na solicitação da API. Por padrão, um conjunto de resultados de pesquisa identifica recursos de video
, channel
e playlist
correspondentes, mas você também pode configurar consultas para recuperar somente um tipo específico de recurso.
Faça um teste agora ou veja um exemplo.
Solicitação
Solicitação HTTP
GET https://www.googleapis.com/youtube/v3/search
Parâmetros
A tabela a seguir lista os parâmetros que esta consulta suporta. Todos os parâmetros listados são os parâmetros de consulta.
Parâmetros | ||
---|---|---|
Parâmetros obrigatórios | ||
part |
string O parâmetro part especifica uma lista separada por vírgulas de uma ou mais propriedades de recurso search que serão incluídas pela resposta da API. Os nomes part que podem ser incluídos no valor do parâmetro são id e snippet .Se o parâmetro identificar uma propriedade que contenha propriedades criança, estas serão incluídas na resposta. Por exemplo, em um resultado search , a propriedade snippet contém outras propriedades que identificam o título, a descrição, entre outros, do resultado. Se você definir part=snippet , a resposta da API também conterá todas essas propriedades aninhadas. |
|
Filtros (especifique exatamente um dos seguintes parâmetros) | ||
forContentOwner |
boolean Este parâmetro só pode ser usado em uma solicitação autorizada adequadamente. Observação: este parâmetro é destinado exclusivamente a parceiros de conteúdo do YouTube. O parâmetro forContentOwner restringe a pesquisa para recuperar apenas os recursos de propriedade do proprietário do conteúdo especificado pelo parâmetro onBehalfOfContentOwner . O usuário deve ser autenticado usando uma conta do CMS vinculada ao proprietário do conteúdo especificado, e onBehalfOfContentOwner deve ser fornecido. |
|
forMine |
boolean Este parâmetro pode ser usado apenas em uma solicitação autorizada adequadamente. O parâmetro forMine restringe a pesquisa para recuperar apenas vídeos de propriedade do usuário autenticado. Se você definir este parâmetro como true , o valor do parâmetro type também deve ser definido como video . |
|
relatedToVideoId |
string O parâmetro relatedToVideoId recupera uma lista de vídeos relacionados ao vídeo identificado pelo valor do parâmetro. O valor do parâmetro deve ser definido como um ID de vídeo do YouTube e, se você estiver usando este parâmetro, o parâmetro type deverá ser definido como video . |
|
Parâmetros opcionais | ||
channelId |
string O parâmetro channelId indica que a resposta da API deve conter apenas os recursos criados pelo canal. |
|
channelType |
string O parâmetro channelType permite restringir a pesquisa a determinado tipo de canal.Os valores aceitáveis são:
|
|
eventType |
string O parâmetro eventType restringe uma pesquisa para transmitir eventos.Os valores aceitáveis são:
|
|
maxResults |
unsigned integer O parâmetro maxResults especifica o número máximo de itens que devem ser retornados no conjunto de resultados. Os valores aceitáveis são 0 a 50 , inclusive. O valor padrão é 5 . |
|
onBehalfOfContentOwner |
string Este parâmetro só pode ser usado em uma solicitação autorizada adequadamente. Observação: este parâmetro é destinado exclusivamente a parceiros de conteúdo do YouTube. O parâmetro onBehalfOfContentOwner indica que as credenciais de autorização da solicitação identificam um usuário do CMS do YouTube que age em nome do proprietário do conteúdo especificado no valor do parâmetro. Este parâmetro destina-se a parceiros de conteúdo do YouTube que possuem e gerenciam vários canais do YouTube diferentes. Ele permite que os proprietários de conteúdo autentiquem uma vez e tenham acesso a todos os dados de seu canal e de seus vídeos sem ter que fornecer credenciais de autenticação para cada canal. A conta do CMS com a qual o usuário autentica deve estar vinculada ao proprietário do conteúdo do YouTube especificado. |
|
order |
string O parâmetro order especifica o método que será usado para classificar os recursos na resposta da API. O valor padrão é SEARCH_SORT_RELEVANCE .Os valores aceitáveis são:
|
|
pageToken |
string O parâmetro pageToken identifica uma página específica no conjunto de resultados que será retornado. Em uma resposta da API, as propriedades nextPageToken e prevPageToken identificam outras páginas que podem ser recuperadas. |
|
publishedAfter |
datetime O parâmetro publishedAfter indica que a resposta da API deve conter apenas os recursos criados após o tempo especificado. O valor está no formato RFC 3339 de dia/hora (1970-01-01T00:00:00Z). |
|
publishedBefore |
datetime O parâmetro publishedBefore indica que a resposta da API deve conter apenas os recursos criados antes do tempo especificado. O valor está no formato RFC 3339 de dia/hora (1970-01-01T00:00:00Z). |
|
q |
string O parâmetro q especifica o termo de consulta a ser pesq uisado. |
|
regionCode |
string O parâmetro regionCode instrui a API para retornar resultados de pesquisa para o país especificado. O valor do parâmetro é um código de país ISO 3166-1 Alpha 2. |
|
safeSearch |
string O parâmetro safeSearch indica se os resultados da pesquisa deverão incluir conteúdo restrito e conteúdo padrãoOs valores aceitáveis são:
|
|
topicId |
string O parâmetro topicId indica que a resposta da API deve conter apenas os recursos associados ao tópico especificado. O valor identifica um ID de tópico Freebase. |
|
type |
string O parâmetro type restringe uma consulta de pesquisa para recuperar somente determinado tipo de recurso. O valor é uma lista separada por vírgulas dos tipos de recursos. O valor padrão é video,channel,playlist .Os valores aceitáveis são:
|
|
videoCaption |
string O parâmetro videoCaption indica se a API deve filtrar os resultados da pesquisa de vídeo com base na existência de legendas ou não.Os valores aceitáveis são:
|
|
videoCategoryId |
string O parâmetro videoCategoryId filtra os resultados da pesquisa de vídeo com base em sua categoria. |
|
videoDefinition |
string O parâmetro videoDefinition permite restringir uma pesquisa para incluir apenas vídeos de alta definição (HD) ou de definição padrão (SD). Vídeos em HD estão disponíveis para reprodução com, pelo menos, 720 p, embora resoluções mais altas, como 1.080 p, também estejam disponíveis.Os valores aceitáveis são:
|
|
videoDimension |
string O parâmetro videoDimension permite restringir uma pesquisa para recuperar somente vídeos em 2D ou 3D.Os valores aceitáveis são:
|
|
videoDuration |
string O parâmetro videoDuration filtra os resultados da pesquisa de vídeo com base em sua duração.Os valores aceitáveis são:
|
|
videoEmbeddable |
string O parâmetro videoEmbeddable permite restringir uma pesquisa para apenas os vídeos que podem ser incorporados a uma página da Web.Os valores aceitáveis são:
|
|
videoLicense |
string O parâmetro videoLicense filtra os resultados da pesquisa filtros para incluir apenas os vídeos com uma determinada licença. O YouTube permite que os usuários que fazem upload anexem a licença Creative Commons ou a licença padrão do YouTube a cada um de seus vídeos.Os valores aceitáveis são:
|
|
videoSyndicated |
string O parâmetro videoSyndicated permite restringir uma pesquisa para apenas os vídeos que podem ser reproduzidos fora do youtube.com.Os valores aceitáveis são:
|
|
videoType |
string O parâmetro videoType permite restringir uma pesquisa a um tipo específico de vídeos.Os valores aceitáveis são:
|
Corpo de solicitação
Não forneça um corpo de solicitação ao chamar este método.
Resposta
Se for bem-sucedido, este método retorna um corpo de resposta com a seguinte estrutura:
{ "kind": "youtube#searchListResponse", "etag": etag, "nextPageToken": string, "prevPageToken": string, "pageInfo": { "totalResults": integer, "resultsPerPage": integer }, "items": [ search Resource ] }
Propriedades
A tabela a seguir define as propriedades que aparecem no resultado da busca:
Propriedades | |
---|---|
kind |
string O tipo do recurso da API. O valor será youtube#searchListResponse . |
etag |
etag A Etag deste recurso. |
nextPageToken |
string O token que pode ser utilizado como o valor do parâmetro pageToken para recuperar a página seguinte do conjunto de resultados. |
prevPageToken |
string O token que pode ser utilizado como o valor do parâmetro pageToken para recuperar a página anterior do conjunto de resultados. |
pageInfo |
object O objeto pageInfo encapsula informações de paginação para o conjunto de resultados. |
pageInfo.totalResults |
integer O número total de resultados no conjunto de resultados. |
pageInfo.resultsPerPage |
integer O número de resultados incluídos na resposta da API. |
items[] |
list A lista de resultados que correspondam aos critérios de pesquisa. |
Exemplos
Observação: os exemplos de código a seguir não representam todas as linguagens de programação suportadas. Consulte a documentação bibliotecas clientes para uma lista das linguagens suportadas.
Apps Script #1
O exemplo de código a seguir são de um aplicativo que chama o método search.list
da API para recuperar os resultados da pesquisa associados a uma determinada palavra-chave.
/** * This function searches for videos related to the keyword 'dogs'. The video IDs and titles * of the search results are logged to Apps Script's log. * * Note that this sample limits the results to 25. To return more results, pass * additional parameters as documented here: * https://developers.google.com/youtube/v3/docs/search/list */ function searchByKeyword() { var results = YouTube.Search.list('id,snippet', {q: 'dogs', maxResults: 25}); for(var i in results.items) { var item = results.items[i]; Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title); } }
Apps Script #2
O exemplo de código a seguir são de um aplicativo que usa a API Freebase e YouTube Data API para recuperar resultados da pesquisa. Primeiramente, o código usa a API Freebase para recuperar uma lista de tópicos que correspondem a uma consulta inserida pelo usuário. Em seguida, o usuário pode selecionar um tópico, e o código chama o método search.list
da API para recuperar vídeos do YouTube relacionados a esse tópico.
/** * This function searches for videos that are associated with a particular Freebase * topic, logging their video IDs and titles to the Apps Script log. This example uses * the topic ID for Google Apps Script. * * Note that this sample limits the results to 25. To return more results, pass * additional parameters as documented here: * https://developers.google.com/youtube/v3/docs/search/list */ function searchByTopic() { var mid = '/m/0gjf126'; var results = YouTube.Search.list('id,snippet', {topicId: mid, maxResults: 25}); for(var i in results.items) { var item = results.items[i]; Logger.log('[%s] Title: %s', item.id.videoId, item.snippet.title); } }
Go #1
O exemplo de código a seguir são de um aplicativo que chama o método search.list
da API para recuperar os resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente Go.
package main import ( "flag" "fmt" "log" "net/http" "google.golang.org/api/googleapi/transport" "google.golang.org/api/youtube/v3" ) var ( query = flag.String("query", "Google", "Search term") maxResults = flag.Int64("max-results", 25, "Max YouTube results") ) const developerKey = "YOUR DEVELOPER KEY" func main() { flag.Parse() client := &http.Client{ Transport: &transport.APIKey{Key: developerKey}, } service, err := youtube.New(client) if err != nil { log.Fatalf("Error creating new YouTube client: %v", err) } // Make the API call to YouTube. call := service.Search.List("id,snippet"). Q(*query). MaxResults(*maxResults) response, err := call.Do() if err != nil { log.Fatalf("Error making search API call: %v", err) } // Group video, channel, and playlist results in separate lists. videos := make(map[string]string) channels := make(map[string]string) playlists := make(map[string]string) // Iterate through each item and add it to the correct list. for _, item := range response.Items { switch item.Id.Kind { case "youtube#video": videos[item.Id.VideoId] = item.Snippet.Title case "youtube#channel": channels[item.Id.ChannelId] = item.Snippet.Title case "youtube#playlist": playlists[item.Id.PlaylistId] = item.Snippet.Title } } printIDs("Videos", videos) printIDs("Channels", channels) printIDs("Playlists", playlists) } // Print the ID and title of each result in a list as well as a name that // identifies the list. For example, print the word section name "Videos" // above a list of video search results, followed by the video ID and title // of each matching video. func printIDs(sectionName string, matches map[string]string) { fmt.Printf("%v:\n", sectionName) for id, title := range matches { fmt.Printf("[%v] %v\n", id, title) } fmt.Printf("\n\n") }
Go #2
O exemplo de código a seguir são de um aplicativo que usa a API Freebase e YouTube Data API para recuperar resultados da pesquisa. Primeiramente, o código usa a API Freebase para recuperar uma lista de tópicos que correspondem a uma consulta inserida pelo usuário. Em seguida, o usuário pode selecionar um tópico, e o código chama o método search.list
da API para recuperar vídeos do YouTube relacionados a esse tópico.
Este exemplo usa a biblioteca cliente Go.
package main import ( "encoding/json" "errors" "flag" "fmt" "io/ioutil" "log" "net/http" "net/url" "os" "google.golang.org/api/googleapi/transport" "google.golang.org/api/youtube/v3" ) var ( query = flag.String("query", "Google", "Freebase search term") maxResults = flag.Int64("max-results", 25, "Max YouTube results") resultType = flag.String("type", "channel", "YouTube result type: video, playlist, or channel") ) const developerKey = "YOUR DEVELOPER KEY HERE" const freebaseSearchURL = "https://www.googleapis.com/freebase/v1/search?%s" // Notable is struct for unmarshalling JSON values from the API. type Notable struct { Name string ID string } // FreebaseTopic is struct for unmarshalling JSON values from the API. type FreebaseTopic struct { Mid string ID string Name string Notable Notable Lang string Score float64 } // FreebaseResponse is struct for unmarshalling JSON values from the Freebase API. type FreebaseResponse struct { Status string Result []FreebaseTopic } func main() { flag.Parse() topicID, err := getTopicID(*query) if err != nil { log.Fatalf("Cannot fetch topic ID from Freebase: %v", err) } err = youtubeSearch(topicID) if err != nil { log.Fatalf("Cannot make YouTube API call: %v", err) } } // getTopicID queries Freebase with the given string. It then prompts the user // to select a topic, then returns the selected topic so that the topic can be // used to search YouTube for videos, channels or playlists. func getTopicID(topic string) (string, error) { urlParams := url.Values{ "query": []string{topic}, "key": []string{developerKey}, } apiURL := fmt.Sprintf(freebaseSearchURL, urlParams.Encode()) resp, err := http.Get(apiURL) if err != nil { return "", err } else if resp.StatusCode != http.StatusOK { errorMsg := fmt.Sprintf("Received HTTP status code %v using developer key: %v", resp.StatusCode, developerKey) return "", errors.New(errorMsg) } body, err := ioutil.ReadAll(resp.Body) if err != nil { return "", err } var data FreebaseResponse err = json.Unmarshal(body, &data) if err != nil { return "", nil } if len(data.Result) == 0 { return "", errors.New("No matching terms were found in Freebase.") } // Print a list of topics for the user to select. fmt.Println("The following topics were found:") for index, topic := range data.Result { if topic.Notable.Name == "" { topic.Notable.Name = "Unknown" } fmt.Printf(" %2d. %s (%s)\r\n", index+1, topic.Name, topic.Notable.Name) } prompt := fmt.Sprintf("Enter a topic number to find related YouTube %s [1-%v]: ", *resultType, len(data.Result)) selection, err := readInt(prompt, 1, len(data.Result)) if err != nil { return "", nil } choice := data.Result[selection-1] return choice.Mid, nil } // readInt reads an integer from standard input and verifies that the value // is between the allowed min and max values (inclusive). func readInt(prompt string, min int, max int) (int, error) { // Loop until we have a valid input. for { fmt.Print(prompt) var i int _, err := fmt.Fscan(os.Stdin, &i) if err != nil { return 0, err } if i < min || i > max { fmt.Println("Invalid input.") continue } return i, nil } } // youtubeSearch searches YouTube for the topic given in the query flag and // prints the results. This function takes a mid parameter, which specifies // a value retrieved using the Freebase API. func youtubeSearch(mid string) error { client := &http.Client{ Transport: &transport.APIKey{Key: developerKey}, } service, err := youtube.New(client) if err != nil { return err } // Make the API call to YouTube. call := service.Search.List("id,snippet"). TopicId(mid). Type(*resultType). MaxResults(*maxResults) response, err := call.Do() if err != nil { return err } // Iterate through each item and output it. for _, item := range response.Items { itemID := "" switch item.Id.Kind { case "youtube#video": itemID = item.Id.VideoId case "youtube#channel": itemID = item.Id.ChannelId case "youtube#playlist": itemID = item.Id.PlaylistId } fmt.Printf("%v (%v)\r\n", item.Snippet.Title, itemID) } return nil }
Java #1
O exemplo de código a seguir são de um aplicativo que chama o método search.list
da API para recuperar os resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente Java.
/* * Copyright (c) 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the License * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express * or implied. See the License for the specific language governing permissions and limitations under * the License. */ package com.google.api.services.samples.youtube.cmdline.data; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.samples.youtube.cmdline.Auth; import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.model.ResourceId; import com.google.api.services.youtube.model.SearchListResponse; import com.google.api.services.youtube.model.SearchResult; import com.google.api.services.youtube.model.Thumbnail; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.Iterator; import java.util.List; import java.util.Properties; /** * Print a list of videos matching a search term. * * @author Jeremy Walker */ public class Search { /** * Define a global variable that identifies the name of a file that * contains the developer's API key. */ private static final String PROPERTIES_FILENAME = "youtube.properties"; private static final long NUMBER_OF_VIDEOS_RETURNED = 25; /** * Define a global instance of a Youtube object, which will be used * to make YouTube Data API requests. */ private static YouTube youtube; /** * Initialize a YouTube object to search for videos on YouTube. Then * display the name and thumbnail image of each video in the result set. * * @param args command line args. */ public static void main(String[] args) { // Read the developer key from the properties file. 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 { // This object is used to make YouTube Data API requests. The last // argument is required, but since 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(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { public void initialize(HttpRequest request) throws IOException { } }).setApplicationName("youtube-cmdline-search-sample").build(); // Prompt the user to enter a query term. String queryTerm = getInputQuery(); // Define the API request for retrieving search results. YouTube.Search.List search = youtube.search().list("id,snippet"); // Set your developer key from the Google API Console for // non-authenticated requests. See: // https://console.cloud.google.com/ String apiKey = properties.getProperty("youtube.apikey"); search.setKey(apiKey); search.setQ(queryTerm); // Restrict the search results to only include videos. See: // https://developers.google.com/youtube/v3/docs/search/list#type search.setType("video"); // 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(NUMBER_OF_VIDEOS_RETURNED); // Call the API and print results. SearchListResponse searchResponse = search.execute(); List<SearchResult> searchResultList = searchResponse.getItems(); if (searchResultList != null) { prettyPrint(searchResultList.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(); } } /* * Prompt the user to enter a query term and return the user-specified term. */ 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) { // Use the string "YouTube Developers Live" as a default. inputQuery = "YouTube Developers Live"; } return inputQuery; } /* * Prints out all results in the Iterator. For each result, print the * title, video ID, and thumbnail. * * @param iteratorSearchResults Iterator of SearchResults to print * * @param query Search query (String) */ private static void prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query) { System.out.println("\n============================================================="); System.out.println( " First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\"."); System.out.println("=============================================================\n"); if (!iteratorSearchResults.hasNext()) { System.out.println(" There aren't any results for your query."); } while (iteratorSearchResults.hasNext()) { SearchResult singleVideo = iteratorSearchResults.next(); ResourceId rId = singleVideo.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 = singleVideo.getSnippet().getThumbnails().getDefault(); System.out.println(" Video Id" + rId.getVideoId()); System.out.println(" Title: " + singleVideo.getSnippet().getTitle()); System.out.println(" Thumbnail: " + thumbnail.getUrl()); System.out.println("\n-------------------------------------------------------------\n"); } } } }
Java #2
O exemplo de código a seguir são de um aplicativo que usa a API Freebase e YouTube Data API para recuperar resultados da pesquisa. Primeiramente, o código usa a API Freebase para recuperar uma lista de tópicos que correspondem a uma consulta inserida pelo usuário. Em seguida, o usuário pode selecionar um tópico, e o código chama o método search.list
da API para recuperar vídeos do YouTube relacionados a esse tópico.
Este exemplo usa a biblioteca cliente Java.
/* * Copyright (c) 2012 Google Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except * in compliance with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under the * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing permissions and * limitations under the License. */ package com.google.api.services.samples.youtube.cmdline.data; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpRequest; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.services.samples.youtube.cmdline.Auth; import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.model.ResourceId; import com.google.api.services.youtube.model.SearchListResponse; import com.google.api.services.youtube.model.SearchResult; import com.google.api.services.youtube.model.Thumbnail; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.utils.URLEncodedUtils; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.codehaus.jackson.JsonNode; import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.node.ArrayNode; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Properties; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * This application demonstrates a semantic YouTube search that prompts the * user to enter a search term and select a topic related to that term. The * class calls the Freebase API to get a topic ID, then passes that id along * with another user query term to the YouTube APIs. The result is a list of * videos based on a semantic search. * * @author Jeremy Walker */ public class Topics { /** * Define a global variable that identifies the name of a file that * contains the developer's API key. */ private static final String PROPERTIES_FILENAME = "youtube.properties"; /** * Define a global variable that specifies the maximum number of videos * that an API response can contain. */ private static final long NUMBER_OF_VIDEOS_RETURNED = 5; /** * Define a global variable that specifies the maximum number of topics * that an API response can contain. */ private static final long NUMBER_OF_TOPICS_RETURNED = 5; /** * Define a global instance of a Youtube object, which will be used * to make YouTube Data API requests. */ private static YouTube youtube; /** * Execute a search request that starts by calling the Freebase API to * retrieve a topic ID matching a user-provided term. Then initialize a * YouTube object to search for YouTube videos and call the YouTube Data * API's youtube.search.list method to retrieve a list of videos associated * with the selected Freebase topic and with another search query term, * which the user also enters. Finally, display the titles, video IDs, and * thumbnail images for the first five videos in the YouTube Data API * response. * * @param args This application does not use command line arguments. */ public static void main(String[] args) { // Read the developer key from the properties file. Properties properties = new Properties(); try { InputStream in = Topics.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 { // Retrieve a Freebase topic ID based on a user-entered query term. String topicsId = getTopicId(); if (topicsId.length() < 1) { System.out.println("No topic id will be applied to your search."); } // Prompt the user to enter a search query term. This term will be // used to retrieve YouTube search results related to the topic // selected above. String queryTerm = getInputQuery("search"); // This object is used to make YouTube Data API requests. The last // argument is required, but since 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(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() { public void initialize(HttpRequest request) throws IOException { } }) .setApplicationName("youtube-cmdline-search-sample") .build(); YouTube.Search.List search = youtube.search().list("id,snippet"); // Set your developer key from the Google API Console for // non-authenticated requests. See: // https://console.cloud.google.com/ String apiKey = properties.getProperty("youtube.apikey"); search.setKey(apiKey); search.setQ(queryTerm); if (topicsId.length() > 0) { search.setTopicId(topicsId); } // Restrict the search results to only include videos. See: // https://developers.google.com/youtube/v3/docs/search/list#type search.setType("video"); // 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(NUMBER_OF_VIDEOS_RETURNED); SearchListResponse searchResponse = search.execute(); List<SearchResult> searchResultList = searchResponse.getItems(); if (searchResultList != null) { prettyPrint(searchResultList.iterator(), queryTerm, topicsId); } else { System.out.println("There were no results for your query."); } } catch (GoogleJsonResponseException e) { System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage()); e.printStackTrace(); } } /* * Prompt the user to enter a search term and return the user's input. * * @param searchCategory This value is displayed to the user to clarify the information that the application is requesting. */ private static String getInputQuery(String searchCategory) throws IOException { String inputQuery = ""; BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); do { System.out.print("Please enter a " + searchCategory + " term: "); inputQuery = bReader.readLine(); } while (inputQuery.length() < 1); return inputQuery; } /** * Retrieve Freebase topics that match a user-provided query term. Then * prompt the user to select a topic and return its topic ID. */ private static String getTopicId() throws IOException { // The application will return an empty string if no matching topic ID // is found or no results are available. String topicsId = ""; // Prompt the user to enter a query term for finding Freebase topics. String topicQuery = getInputQuery("topics"); // The Freebase Java library does not provide search functionality, so // the application needs to call directly against the URL. This code // constructs the proper URL, then uses jackson classes to convert the // JSON response into a Java object. You can learn more about the // Freebase search calls at: http://wiki.freebase.com/wiki/ApiSearch. HttpClient httpclient = new DefaultHttpClient(); List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("query", topicQuery)); params.add(new BasicNameValuePair("limit", Long.toString(NUMBER_OF_TOPICS_RETURNED))); String serviceURL = "https://www.googleapis.com/freebase/v1/search"; String url = serviceURL + "?" + URLEncodedUtils.format(params, "UTF-8"); HttpResponse httpResponse = httpclient.execute(new HttpGet(url)); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // Convert the JSON to an object. This code does not do an // exact map from JSON to POJO (Plain Old Java object), but // you could create additional classes and use them with the // mapper.readValue() function to get that exact mapping. ObjectMapper mapper = new ObjectMapper(); JsonNode rootNode = mapper.readValue(instream, JsonNode.class); // Confirm that the HTTP request was handled successfully by // checking the API response's HTTP response code. if (rootNode.get("status").asText().equals("200 OK")) { // In the API response, the "result" field contains the // list of needed results. ArrayNode arrayNodeResults = (ArrayNode) rootNode.get("result"); // Prompt the user to select a topic from the list of API // results. topicsId = getUserChoice(arrayNodeResults); } } finally { instream.close(); } } return topicsId; } /** * Output results from the Freebase topic search to the user, prompt the * user to select a topic, and return the appropriate topic ID. * * @param freebaseResults ArrayNode This object contains the search results. */ private static String getUserChoice(ArrayNode freebaseResults) throws IOException { String freebaseId = ""; if (freebaseResults.size() < 1) { return freebaseId; } // Print a list of topics retrieved from Freebase. for (int i = 0; i < freebaseResults.size(); i++) { JsonNode node = freebaseResults.get(i); System.out.print(" " + i + " = " + node.get("name").asText()); if (node.get("notable") != null) { System.out.print(" (" + node.get("notable").get("name").asText() + ")"); } System.out.println(""); } BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); String inputChoice; // Prompt the user to select a topic. do { System.out.print("Choose the number of the Freebase Node: "); inputChoice = bReader.readLine(); } while (!isValidIntegerSelection(inputChoice, freebaseResults.size())); // Return the topic ID needed for the API request that retrieves // YouTube search results. JsonNode node = freebaseResults.get(Integer.parseInt(inputChoice)); freebaseId = node.get("mid").asText(); return freebaseId; } /** * Confirm that the string represents a valid, positive integer, that is * less than or equal to 999,999,999. (Since the API will not return a * billion results for a query, any integer that the user enters will need * to be less than that to be valid, anyway.) * * @param input The string to test. * @param max The integer must be less then this maximum number. */ public static boolean isValidIntegerSelection(String input, int max) { if (input.length() > 9) return false; boolean validNumber = false; // Only accept positive numbers with a maximum of nine digits. Pattern intsOnly = Pattern.compile("^\\d{1,9}$"); Matcher makeMatch = intsOnly.matcher(input); if (makeMatch.find()) { int number = Integer.parseInt(makeMatch.group()); if ((number >= 0) && (number < max)) { validNumber = true; } } return validNumber; } /* * Prints out all results in the Iterator. For each result, print the * title, video ID, and thumbnail. * * @param iteratorSearchResults Iterator of SearchResults to print * @param query Search query (String) */ private static void prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query, String topicsId) { System.out.println("\n============================================================="); System.out.println(" First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\" with Topics id: " + topicsId + "."); System.out.println("=============================================================\n"); if (!iteratorSearchResults.hasNext()) { System.out.println(" There aren't any results for your query."); } while (iteratorSearchResults.hasNext()) { SearchResult singleVideo = iteratorSearchResults.next(); ResourceId rId = singleVideo.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 = singleVideo.getSnippet().getThumbnails().getDefault(); System.out.println(" Video Id" + rId.getVideoId()); System.out.println(" Title: " + singleVideo.getSnippet().getTitle()); System.out.println(" Thumbnail: " + thumbnail.getUrl()); System.out.println("\n-------------------------------------------------------------\n"); } } } }
JavaScript
O código JavaScript abaixo executa uma pesquisa no YouTube para o termo da consulta especificada e preenche um elemento DOM com a resposta da API. Novamente, você precisa atualizar o ID do cliente no arquivo auth.js
para executar este código.
Este exemplo usa a biblioteca cliente JavaScript.
// After the API loads, call a function to enable the search box. function handleAPILoaded() { $('#search-button').attr('disabled', false); } // Search for a specified string. function search() { var q = $('#query').val(); var request = gapi.client.youtube.search.list({ q: q, part: 'snippet' }); request.execute(function(response) { var str = JSON.stringify(response.result); $('#search-container').html('<pre>' + str + '</pre>'); }); }
.NET
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente .NET.
/* */ using System; using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading; using System.Threading.Tasks; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.Upload; using Google.Apis.Util.Store; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; namespace Google.Apis.YouTube.Samples { /// <summary> /// YouTube Data API v3 sample: search by keyword. /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher. /// See https://developers.google.com/api-client-library/dotnet/get_started /// /// Set ApiKey to the API key value from the APIs & auth > Registered apps tab of /// https://cloud.google.com/console /// Please ensure that you have enabled the YouTube Data API for your project. /// </summary> internal class Search { [STAThread] static void Main(string[] args) { Console.WriteLine("YouTube Data API: Search"); Console.WriteLine("========================"); try { new Search().Run().Wait(); } catch (AggregateException ex) { foreach (var e in ex.InnerExceptions) { Console.WriteLine("Error: " + e.Message); } } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } private async Task Run() { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "REPLACE_ME", ApplicationName = this.GetType().ToString() }); var searchListRequest = youtubeService.Search.List("snippet"); searchListRequest.Q = "Google"; // Replace with your search term. searchListRequest.MaxResults = 50; // Call the search.list method to retrieve results matching the specified query term. var searchListResponse = await searchListRequest.ExecuteAsync(); List<string> videos = new List<string>(); List<string> channels = new List<string>(); List<string> playlists = new List<string>(); // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)); break; case "youtube#channel": channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId)); break; case "youtube#playlist": playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId)); break; } } Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos))); Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels))); Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists))); } } }
PHP
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente PHP.
<?php /** * Library Requirements * * 1. Install composer (https://getcomposer.org) * 2. On the command line, change to this directory (api-samples/php) * 3. Require the google/apiclient library * $ composer require google/apiclient:~2.0 */ if (!file_exists(__DIR__ . '/vendor/autoload.php')) { throw new \Exception('please run "composer require google/apiclient:~2.0" in "' . __DIR__ .'"'); } require_once __DIR__ . '/vendor/autoload.php'; $htmlBody = <<<END <form method="GET"> <div> Search Term: <input type="search" id="q" name="q" placeholder="Enter Search Term"> </div> <div> Max Results: <input type="number" id="maxResults" name="maxResults" min="1" max="50" step="1" value="25"> </div> <input type="submit" value="Search"> </form> END; // This code will execute if the user entered a search query in the form // and submitted the form. Otherwise, the page displays the form above. if (isset($_GET['q']) && isset($_GET['maxResults'])) { /* * Set $DEVELOPER_KEY to the "API key" value from the "Access" tab of the * Google API Console <https://console.cloud.google.com/> * Please ensure that you have enabled the YouTube Data API for your project. */ $DEVELOPER_KEY = 'REPLACE_ME'; $client = new Google_Client(); $client->setDeveloperKey($DEVELOPER_KEY); // Define an object that will be used to make all API requests. $youtube = new Google_Service_YouTube($client); $htmlBody = ''; try { // Call the search.list method to retrieve results matching the specified // query term. $searchResponse = $youtube->search->listSearch('id,snippet', array( 'q' => $_GET['q'], 'maxResults' => $_GET['maxResults'], )); $videos = ''; $channels = ''; $playlists = ''; // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach ($searchResponse['items'] as $searchResult) { switch ($searchResult['id']['kind']) { case 'youtube#video': $videos .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], $searchResult['id']['videoId']); break; case 'youtube#channel': $channels .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], $searchResult['id']['channelId']); break; case 'youtube#playlist': $playlists .= sprintf('<li>%s (%s)</li>', $searchResult['snippet']['title'], $searchResult['id']['playlistId']); break; } } $htmlBody .= <<<END <h3>Videos</h3> <ul>$videos</ul> <h3>Channels</h3> <ul>$channels</ul> <h3>Playlists</h3> <ul>$playlists</ul> END; } catch (Google_Service_Exception $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>', htmlspecialchars($e->getMessage())); } } ?> <!doctype html> <html> <head> <title>YouTube Search</title> </head> <body> <?=$htmlBody?> </body> </html>
Python #1
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente Python.
#!/usr/bin/python from apiclient.discovery import build from apiclient.errors import HttpError from oauth2client.tools import argparser # Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps # tab of # https://cloud.google.com/console # Please ensure that you have enabled the YouTube Data API for your project. DEVELOPER_KEY = "REPLACE_ME" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" def youtube_search(options): youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) # Call the search.list method to retrieve results matching the specified # query term. search_response = youtube.search().list( q=options.q, part="id,snippet", maxResults=options.max_results ).execute() videos = [] channels = [] playlists = [] # Add each result to the appropriate list, and then display the lists of # matching videos, channels, and playlists. for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"])) elif search_result["id"]["kind"] == "youtube#channel": channels.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"])) elif search_result["id"]["kind"] == "youtube#playlist": playlists.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"])) print "Videos:\n", "\n".join(videos), "\n" print "Channels:\n", "\n".join(channels), "\n" print "Playlists:\n", "\n".join(playlists), "\n" if __name__ == "__main__": argparser.add_argument("--q", help="Search term", default="Google") argparser.add_argument("--max-results", help="Max results", default=25) args = argparser.parse_args() try: youtube_search(args) except HttpError, e: print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Python #2
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a um determinado tópico do Freebase.
Este exemplo usa a biblioteca cliente Python.
#!/usr/bin/python from apiclient.discovery import build from apiclient.errors import HttpError from oauth2client.tools import argparser import json import urllib # Set DEVELOPER_KEY to the API key value from the APIs & auth > Registered apps # tab of # https://cloud.google.com/console # Please ensure that you have enabled the YouTube Data API for your project. DEVELOPER_KEY = "REPLACE_ME" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s" def get_topic_id(options): # Retrieve a list of Freebase topics associated with the provided query term. freebase_params = dict(query=options.query, key=DEVELOPER_KEY) freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params) freebase_response = json.loads(urllib.urlopen(freebase_url).read()) if len(freebase_response["result"]) == 0: exit("No matching terms were found in Freebase.") # Display the list of matching Freebase topics. mids = [] index = 1 print "The following topics were found:" for result in freebase_response["result"]: mids.append(result["mid"]) print " %2d. %s (%s)" % (index, result.get("name", "Unknown"), result.get("notable", {}).get("name", "Unknown")) index += 1 # Display a prompt for the user to select a topic and return the topic ID # of the selected topic. mid = None while mid is None: index = raw_input("Enter a topic number to find related YouTube %ss: " % options.type) try: mid = mids[int(index) - 1] except ValueError: pass return mid def youtube_search(mid, options): youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) # Call the search.list method to retrieve results associated with the # specified Freebase topic. search_response = youtube.search().list( topicId=mid, type=options.type, part="id,snippet", maxResults=options.max_results ).execute() # Print the title and ID of each matching resource. for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": print "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"]) elif search_result["id"]["kind"] == "youtube#channel": print "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"]) elif search_result["id"]["kind"] == "youtube#playlist": print "%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"]) if __name__ == "__main__": argparser.add_argument("--query", help="Freebase search term", default="Google") argparser.add_argument("--max-results", help="Max YouTube results", default=25) argparser.add_argument("--type", help="YouTube result type: video, playlist, or channel", default="channel") args = argparser.parse_args() mid = get_topic_id(args) try: youtube_search(mid, args) except HttpError, e: print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
Python App Engine #1
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a uma determinada palavra-chave.
import os import urllib import webapp2 import jinja2 from apiclient.discovery import build from optparse import OptionParser JINJA_ENVIRONMENT = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.autoescape']) # Set DEVELOPER_KEY to the "API key" value from the Google Developers Console: # https://console.developers.google.com/project/_/apiui/credential # Please ensure that you have enabled the YouTube Data API for your project. DEVELOPER_KEY = "REPLACE_ME" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" class MainHandler(webapp2.RequestHandler): def get(self): if DEVELOPER_KEY == "REPLACE_ME": self.response.write("""You must set up a project and get an API key to run this project. Please visit <landing page> to do so.""" else: youtube = build( YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) search_response = youtube.search().list( q="Hello", part="id,snippet", maxResults=5 ).execute() videos = [] channels = [] playlists = [] for search_result in search_response.get("items", []): if search_result["id"]["kind"] == "youtube#video": videos.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["videoId"])) elif search_result["id"]["kind"] == "youtube#channel": channels.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["channelId"])) elif search_result["id"]["kind"] == "youtube#playlist": playlists.append("%s (%s)" % (search_result["snippet"]["title"], search_result["id"]["playlistId"])) template_values = { 'videos': videos, 'channels': channels, 'playlists': playlists } self.response.headers['Content-type'] = 'text/plain' template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values)) app = webapp2.WSGIApplication([ ('/.*', MainHandler), ], debug=True)
Python App Engine #2
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a um determinado tópico do Freebase.
import os import urllib import webapp2 import jinja2 from apiclient.discovery import build from optparse import OptionParser import json JINJA_ENVIRONMENT = jinja2.Environment( loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), extensions=['jinja2.ext.autoescape']) REGISTRATION_INSTRUCTIONS = """ You must set up a project and get an API key to run this code. Please see the instructions for creating a project and a key at <a href="https://developers.google.com/youtube/registering_an_application" >https://developers.google.com/youtube/registering_an_application</a>. <br><br> Make sure that you have enabled the YouTube Data API (v3) and the Freebase API for your project.""" # Set API_KEY to the "API key" value from the Google Developers Console: # https://console.developers.google.com/project/_/apiui/credential # Please ensure that you have enabled the YouTube Data API and Freebase API # for your project. API_KEY = "REPLACE_ME" YOUTUBE_API_SERVICE_NAME = "youtube" YOUTUBE_API_VERSION = "v3" FREEBASE_SEARCH_URL = "https://www.googleapis.com/freebase/v1/search?%s" QUERY_TERM = "dog" class MainHandler(webapp2.RequestHandler): def get(self): if API_KEY == 'REPLACE_ME': self.response.write(REGISTRATION_INSTRUCTIONS) else: # Present a list of Freebase topic IDs for the query term self.list_topics(QUERY_TERM) def list_topics(self, QUERY_TERM): # Retrieve a list of Freebase topics associated with the query term freebase_params = dict(query=QUERY_TERM, key=API_KEY) freebase_url = FREEBASE_SEARCH_URL % urllib.urlencode(freebase_params) freebase_response = json.loads(urllib.urlopen(freebase_url).read()) if len(freebase_response["result"]) == 0: exit("No matching terms were found in Freebase.") # Create a page that shows a select box listing the topics. # When the user selects a topic and submits the form, the # 'post' method below will handle the form submission and # retrieve videos for the selected topic. select_topic_page = (''' <html> <body> <p>The following topics were found:</p> <form method="post"> <select name="topic"> ''') for result in freebase_response["result"]: select_topic_page += ('<option value="' + result["mid"] + '">' + result.get("name", "Unknown") + '</option>') select_topic_page += ''' </select> <p><input type="submit" /></p> </form> </body> </html> ''' # Display the HTML page listing the topic choices. self.response.out.write(select_topic_page) def post(self): topic_id = self.request.get('topic') # Service for calling the YouTube API youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=API_KEY) # Execute the search request using default query term and retrieved topic. search_response = youtube.search().list( part = 'id,snippet', type = 'video', topicId = topic_id ).execute() videos = [] for search_result in search_response.get("items", []): videos.append(search_result) template_values = { 'videos': videos } self.response.headers['Content-type'] = 'text/html' template = JINJA_ENVIRONMENT.get_template('index.html') self.response.write(template.render(template_values)) app = webapp2.WSGIApplication([ ('/.*', MainHandler), ], debug=True)
Ruby
O exemplo de código abaixo chama o método search.list
da API para recuperar resultados da pesquisa associados a uma determinada palavra-chave.
Este exemplo usa a biblioteca cliente Ruby.
#!/usr/bin/ruby require 'rubygems' gem 'google-api-client', '>0.7' require 'google/api_client' require 'trollop' # Set DEVELOPER_KEY to the API key value from the APIs & auth > Credentials # tab of # Google API Console <https://console.cloud.google.com/> # Please ensure that you have enabled the YouTube Data API for your project. DEVELOPER_KEY = 'REPLACE_ME' YOUTUBE_API_SERVICE_NAME = 'youtube' YOUTUBE_API_VERSION = 'v3' def get_service client = Google::APIClient.new( :key => DEVELOPER_KEY, :authorization => nil, :application_name => $PROGRAM_NAME, :application_version => '1.0.0' ) youtube = client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION) return client, youtube end def main opts = Trollop::options do opt :q, 'Search term', :type => String, :default => 'Google' opt :max_results, 'Max results', :type => :int, :default => 25 end client, youtube = get_service begin # Call the search.list method to retrieve results matching the specified # query term. search_response = client.execute!( :api_method => youtube.search.list, :parameters => { :part => 'snippet', :q => opts[:q], :maxResults => opts[:max_results] } ) videos = [] channels = [] playlists = [] # Add each result to the appropriate list, and then display the lists of # matching videos, channels, and playlists. search_response.data.items.each do |search_result| case search_result.id.kind when 'youtube#video' videos << "#{search_result.snippet.title} (#{search_result.id.videoId})" when 'youtube#channel' channels << "#{search_result.snippet.title} (#{search_result.id.channelId})" when 'youtube#playlist' playlists << "#{search_result.snippet.title} (#{search_result.id.playlistId})" end end puts "Videos:\n", videos, "\n" puts "Channels:\n", channels, "\n" puts "Playlists:\n", playlists, "\n" rescue Google::APIClient::TransmissionError => e puts e.result.body end end main
Erros
A tabela abaixo identifica as mensagens de erro que a API pode retornar em resposta a uma chamada para este método. Consulte a documentação mensagem de erro para mais detalhes.
Tipo de erro | Detalhe do erro | Descrição |
---|---|---|
badRequest |
invalidSearchFilter |
A solicitação contém uma combinação inválida de filtros de pesquisa e/ou restrições. |
badRequest |
invalidVideoId |
O parâmetro relatedToVideo especificou um ID de vídeo inválido. |
Conheça agora.
Use o Explorador API para chamar este método em dados ativos e ver a solicitação e a resposta da API.