使用Java YouTubeService类进行经过身份验证的YouTube API调用时,关键字对象为空

时间:2013-04-22 00:16:30

标签: java youtube-api

我正在尝试从我自己的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 );

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

如果您打算将我推荐给另一个身份验证选项,请展示为我的特定方案使用该其他选项的示例。

2 个答案:

答案 0 :(得分:0)

您正在遇到this blog post中描述的行为:您的API调用违反搜索索引,而这些结果将永远不会包含关键字。

an example显示了如何使用Data API的v2在Java中请求上传Feed;您可以修改该示例以使用频道名称default代替username,并且您将自动为当前经过身份验证的帐户提取上传Feed。

答案 1 :(得分:0)

以下是我最终使用标签(关键字)获取JSON-C Feed的原因:

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