How to search videos with youtube data API in Android
Asked Answered
A

3

8

I'm developing an application in Android which needs to search for videos of YouTube by Keyword. I have used Youtube data API with the code below:

    try {
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {
            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("YoutubeQoE").build();

        // 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
        search.setKey(DeveloperKey.DEVELOPER_KEY);
        search.setQ("dogs");

        // 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(25);

        SearchListResponse searchResponse = search.execute();
        List<SearchResult> lista = searchResponse.getItems();

    } 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();
    }

For de DEVELOPER_KEY I have used a public API access at google developer console.

But when I execute the program there is a problem in the line:

SearchListResponse searchResponse = search.execute();

In the android manifest.xml I have these permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

I would be very grateful if anybody could help me

Antifriction answered 20/2, 2014 at 16:43 Comment(2)
what's the error you are getting?Plasmosome
Dear Josesvm, I am doing the same what you tried.But I could not get supporting libraires for this search java class file.So kindly share the jar files or jar files location.Xerosis
P
2

You can compare your project against YouTube Direct Lite for Android. Yes, instead of OAuth2, setting API key would be enough.

Plasmosome answered 20/2, 2014 at 19:26 Comment(2)
Thank you very much. I have found the problem with your help. Although I'm using it in an android app I should use an API key for web browser.Antifriction
do you have other example or is it only youtube Direct liteWafture
R
13

Another way of achieving this would be getting results from this:

https://www.googleapis.com/youtube/v3/search?part=snippet&q=eminem&type=video&key=<key>

You can play with parameters to get what you actually need. You also need an API key from https://console.developers.google.com/.

Rich answered 12/10, 2014 at 14:1 Comment(1)
Could you please explain this.I am trying to get videos by entering search key word using google youtube api v3 .so I am using the sample code provided by google developers.google.com/youtube/v3/code_samples/… .but I could not get supporting jar files for this sample class.So please tell the suggestion.Xerosis
P
2

You can compare your project against YouTube Direct Lite for Android. Yes, instead of OAuth2, setting API key would be enough.

Plasmosome answered 20/2, 2014 at 19:26 Comment(2)
Thank you very much. I have found the problem with your help. Although I'm using it in an android app I should use an API key for web browser.Antifriction
do you have other example or is it only youtube Direct liteWafture
F
0

Generally, this error occurs when someone tries to perform network calls on UI Thread, which is not allowed in Android. check logcat if you get this error - (error-android-os-networkonmainthreadexception) Go through this answer for the solution to this problem - fix of neworkonmainthreadexception

Familiarity answered 7/7, 2018 at 13:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.