Exemple #1
0
  private void initChannel() {

    ChannelListResponse response = null;
    try {
      channelQuery = youtube.channels().list("id,snippet,brandingSettings,statistics");
      channelQuery.setKey(this.KEY);
      channelQuery.setId(this.channel_id);
      channelQuery.setFields(
          "items(id,brandingSettings,snippet/title,snippet/description,snippet/thumbnails,statistics)");
    } catch (IOException e) {
      Log.d("YC", "Could not init: " + e);
    }

    try {
      response = channelQuery.execute();
    } catch (GoogleJsonResponseException e) {
      Log.d("YC", "Could not search: " + e.getMessage());
      switch (e.getStatusCode()) {
        case 403:
          this.KEY = this.context.getString(R.string.api_key2);
          channelQuery.setKey(this.KEY);
          try {
            response = channelQuery.execute();
          } catch (IOException e1) {
            Log.d("YC-KEY2", "Could not initialize: " + e1.getMessage());
          }
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    List<Channel> results = response.getItems();

    for (Channel result : results) {
      channel.setChannel_id(this.channel_id);
      channel.setTitle(result.getSnippet().getTitle());
      channel.setDescription(result.getSnippet().getDescription());
      channel.setThumbnailURL(result.getSnippet().getThumbnails().getHigh().getUrl());
      if (isTablet(this.context)) {
        channel.setBannerURL(result.getBrandingSettings().getImage().getBannerTabletImageUrl());
      } else {
        channel.setBannerURL(result.getBrandingSettings().getImage().getBannerImageUrl());
      }
      channel.setViewCount(result.getStatistics().getViewCount());
      channel.setVideoCount(result.getStatistics().getVideoCount());
      channel.setSubscriberCount(result.getStatistics().getSubscriberCount());
    }
  }
Exemple #2
0
  /**
   * Authorize the user, call the youtube.channels.list method to retrieve the playlist ID for the
   * list of videos uploaded to the user's channel, and then call the youtube.playlistItems.list
   * method to retrieve the list of videos in that playlist.
   *
   * @param args command line args (not used).
   */
  public static void main(String[] args) {

    // This OAuth 2.0 access scope allows for read-only access to the
    // authenticated user's account, but not other types of account access.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly");

    try {
      // Authorize the request.
      Credential credential = Auth.authorize(scopes, "myuploads");

      // This object is used to make YouTube Data API requests.
      youtube =
          new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential)
              .setApplicationName("youtube-cmdline-myuploads-sample")
              .build();

      // Call the API's channels.list method to retrieve the
      // resource that represents the authenticated user's channel.
      // In the API response, only include channel information needed for
      // this use case. The channel's contentDetails part contains
      // playlist IDs relevant to the channel, including the ID for the
      // list that contains videos uploaded to the channel.
      YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
      channelRequest.setMine(true);
      channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
      ChannelListResponse channelResult = channelRequest.execute();

      List<Channel> channelsList = channelResult.getItems();

      if (channelsList != null) {
        // The user's default channel is the first item in the list.
        // Extract the playlist ID for the channel's videos from the
        // API response.
        String uploadPlaylistId =
            channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();

        // Define a list to store items in the list of uploaded videos.
        List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();

        // Retrieve the playlist of the channel's uploaded videos.
        YouTube.PlaylistItems.List playlistItemRequest =
            youtube.playlistItems().list("id,contentDetails,snippet");
        playlistItemRequest.setPlaylistId(uploadPlaylistId);

        // Only retrieve data used in this application, thereby making
        // the application more efficient. See:
        // https://developers.google.com/youtube/v3/getting-started#partial
        playlistItemRequest.setFields(
            "items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");

        String nextToken = "";

        // Call the API one or more times to retrieve all items in the
        // list. As long as the API response returns a nextPageToken,
        // there are still more items to retrieve.
        do {
          playlistItemRequest.setPageToken(nextToken);
          PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();

          playlistItemList.addAll(playlistItemResult.getItems());

          nextToken = playlistItemResult.getNextPageToken();
        } while (nextToken != null);

        // Prints information about the results.
        prettyPrint(playlistItemList.size(), playlistItemList.iterator());
      }

    } catch (GoogleJsonResponseException e) {
      e.printStackTrace();
      System.err.println(
          "There was a service error: "
              + e.getDetails().getCode()
              + " : "
              + e.getDetails().getMessage());

    } catch (Throwable t) {
      t.printStackTrace();
    }
  }