【问题标题】:Get view count using Google Youtube API使用 Google Youtube API 获取观看次数
【发布时间】:2015-11-06 09:59:22
【问题描述】:

我想获取一组视频的观看次数。以下是我的代码的相关部分。

  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 list = youtube.videos().list("statistics");”行中出现以下错误。

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

【问题讨论】:

    标签: youtube youtube-data-api google-api-java-client


    【解决方案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();
            }
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 2011-03-20
      • 2015-08-30
      • 2019-03-12
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      相关资源
      最近更新 更多