1

我正在尝试从我自己的 YouTube 帐户中获取视频,以便获取每个视频的关键字/标签。我正在尝试使用最简单的方法进行经过身份验证的调用,以获取带有关键字/标签的视频。

这是我的Java代码:

String clientID = "XXXXXXXXXXXX.apps.googleusercontent.com";
String devKey = "MY-DEVELOPER-KEY";
String userEmail = "MY-GMAIL-EMAIL";
String userPassword = "MY-GMAIL-PASSWORD";
String authorName = "MY-YOUTUBE-ACCOUNT-NAME";
String url = "https://gdata.youtube.com/feeds/api/videos";

YouTubeService service = new YouTubeService( clientID, devKey );
service.setUserCredentials( userEmail, userPassword );

YouTubeQuery query = new YouTubeQuery( new URL( url ) );
query.setAuthor( authorName );

VideoFeed videoFeed = service.query( query, VideoFeed.class );

请帮助我了解我做错了什么,以验证并获取这些媒体关键字。

如果您要向我推荐另一个身份验证选项,请展示一个针对我的特定场景使用该其他选项的示例。

4

2 回答 2

0

您遇到了这篇博文中描述的行为:您的 API 调用与搜索索引相反,这些结果中永远不会包含关键字。

一个示例展示了如何使用 v2 的数据 API 请求 Java 中的上传提要;您可以修改该示例以使用频道名称default代替,username您将自动为当前经过身份验证的帐户提取上传提要。

于 2013-04-22T21:02:19.453 回答
0

这是我最终获得带有标签(关键字)的 JSON-C 提要的方法:

    /** AUTHENITICATION **/

    // HTTP connection        
    URL url = new URL("https://www.google.com/accounts/ClientLogin");
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);
    urlConnection.setUseCaches(false);
    urlConnection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");

    // Form the POST parameters
    StringBuilder content = new StringBuilder();
    content.append("Email=").append(URLEncoder.encode("MY-GMAIL-LOGIN", "UTF-8"));
    content.append("&Passwd=").append(URLEncoder.encode("MY-GMAIL-PASSWORD", "UTF-8"));
    content.append("&service=").append(URLEncoder.encode("youtube", "UTF-8"));
    OutputStream outputStream = urlConnection.getOutputStream();
    outputStream.write(content.toString().getBytes("UTF-8"));
    outputStream.close();

    // Check response status
    int responseCode = urlConnection.getResponseCode();
    if( responseCode != HttpURLConnection.HTTP_OK ) {
        // EXCEPTION
    }

    // Get the token from the response
    String token;
    InputStream inputStream = urlConnection.getInputStream();
    BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
    String line = null;
    while((line = in.readLine()) != null) {
        if ( line.indexOf("Auth=") > -1 ) {
            token = line.split("=")[1];
        }
    }

    /** JSON-C FEED WITH TAGS **/

    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod("http://gdata.youtube.com/feeds/api/users/default/uploads?v=2&alt=jsonc&max-results=50&start-index=1");

    // set the authentication headers
    method.setRequestHeader("Authorization", "GoogleLogin auth=" + token);
    method.setRequestHeader("X-GData-Key", "key=MY-DEV-KEY"); 
    method.setRequestHeader("GData-Version", "2");
    method.setRequestHeader("Content-Type", "application/json; charset=UTF-8");

    // Make the call
    int statusCode = client.executeMethod(method);
    if ( statusCode != HttpStatus.SC_OK ) {
        // EXCEPTION
    }
    String JSON = method.getResponseBodyAsString();
于 2013-04-25T17:03:45.323 回答