2015-11-06 44 views
0

我想獲得一組視頻的查看次數。以下是我的代碼的相關部分。使用Google Youtube API獲取查看次數

SearchResult singleVideo = iteratorSearchResults.next(); 
    ResourceId rId = singleVideo.getId(); 

    // Double checks the kind is video. 
    if (rId.getKind().equals("youtube#video")) { 
    Thumbnail thumbnail = singleVideo.getSnippet().getThumbnails().get("default"); 

    System.out.println(" Video Id" + rId.getVideoId()); 
    System.out.println(" Title: " + singleVideo.getSnippet().getTitle()); 
    System.out.println(" Thumbnail: " + thumbnail.getUrl()); 

    YouTube.Videos.List list = youtube.videos().list("statistics"); 
    list.setId(rId.getVideoId()); 
    list.setKey("youtube.apikey");    
    Video v = list.execute().getItems().get(0); 
    System.out.println("The view count is: "+v.getStatistics().getViewCount()); 
    System.out.println("\n-------------------------------------------------------------\n"); 
    } 

這給該行中的下面的錯誤 「(統計」)YouTube.Videos.Lists列表= youtube.videos()列表。 「;」。

error: method list in class YouTube.Videos cannot be applied to given types; 

回答

1

如果這是一個編譯錯誤,那麼您所包含的庫版本可能存在一些問題。我嘗試了youtube API docs的示例代碼,它爲我工作。

我已經從樣品中除去一些額外的代碼來顯示如何觀看次數可以爲單個視頻檢索:

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.Video; 
    import com.google.api.services.youtube.model.VideoListResponse; 

    import java.io.IOException; 
    import java.math.BigInteger; 

public class GeolocationSearch { 
    public static void main(String[] args) { 

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

      String apiKey = "API_KEY"; 
      YouTube.Videos.List listVideosRequest = youtube.videos().list("statistics"); 
      listVideosRequest.setId("lf_wVfwpfp8"); // add list of video IDs here 
      listVideosRequest.setKey(apiKey); 
      VideoListResponse listResponse = listVideosRequest.execute(); 

      Video video = listResponse.getItems().get(0); 

      BigInteger viewCount = video.getStatistics().getViewCount(); 

      System.out.println(" ViewCount: " + viewCount); 
      System.out.println("\n-------------------------------------------------------------\n"); 

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

} 
相關問題