如何在Android中使用YouTube数据API搜索视频 [英] How to search videos with youtube data API in Android

查看:170
本文介绍了如何在Android中使用YouTube数据API搜索视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Android开发一个应用程序,该应用程序需要按关键字搜索YouTube的视频.我已将Youtube数据API与以下代码一起使用:

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

对于DEVELOPER_KEY,我已经在Google开发人员控制台上使用了公共API访问权限.

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

SearchListResponse searchResponse = search.execute();

在android manifest.xml中,我具有以下权限:

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

推荐答案

您可以将您的项目与

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

这篇关于如何在Android中使用YouTube数据API搜索视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆