Search in sources :

Example 1 with Channel

use of com.google.api.services.youtube.model.Channel in project api-samples by youtube.

the class ChannelLocalizations method listChannelLocalizations.

/**
     * Returns a list of localized metadata for a channel.
     *
     * @param channelId The id parameter specifies the channel ID for the resource
     * that is being updated.
     * @throws IOException
     */
private static void listChannelLocalizations(String channelId) throws IOException {
    // Call the YouTube Data API's channels.list method to retrieve channels.
    ChannelListResponse channelListResponse = youtube.channels().list("snippet,localizations").setId(channelId).execute();
    // Since the API request specified a unique channel ID, the API
    // response should return exactly one channel. If the response does
    // not contain a channel, then the specified channel ID was not found.
    List<Channel> channelList = channelListResponse.getItems();
    if (channelList.isEmpty()) {
        System.out.println("Can't find a channel with ID: " + channelId);
        return;
    }
    Channel channel = channelList.get(0);
    Map<String, ChannelLocalization> localizations = channel.getLocalizations();
    // Print information from the API response.
    System.out.println("\n================== Channel ==================\n");
    System.out.println("  - ID: " + channel.getId());
    for (String language : localizations.keySet()) {
        System.out.println("  - Description(" + language + "): " + localizations.get(language).getDescription());
    }
    System.out.println("\n-------------------------------------------------------------\n");
}
Also used : ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse) ChannelLocalization(com.google.api.services.youtube.model.ChannelLocalization) Channel(com.google.api.services.youtube.model.Channel)

Example 2 with Channel

use of com.google.api.services.youtube.model.Channel in project api-samples by youtube.

the class ChannelLocalizations method setChannelLocalization.

/**
     * Updates a channel's default language and sets its localized metadata.
     *
     * @param channelId The id parameter specifies the channel ID for the resource
     * that is being updated.
     * @param defaultLanguage The language of the channel's default metadata
     * @param language The language of the localized metadata
     * @param description The localized description to be set
     * @throws IOException
     */
private static void setChannelLocalization(String channelId, String defaultLanguage, String language, String description) throws IOException {
    // Call the YouTube Data API's channels.list method to retrieve channels.
    ChannelListResponse channelListResponse = youtube.channels().list("brandingSettings,localizations").setId(channelId).execute();
    // Since the API request specified a unique channel ID, the API
    // response should return exactly one channel. If the response does
    // not contain a channel, then the specified channel ID was not found.
    List<Channel> channelList = channelListResponse.getItems();
    if (channelList.isEmpty()) {
        System.out.println("Can't find a channel with ID: " + channelId);
        return;
    }
    Channel channel = channelList.get(0);
    // Modify channel's default language and localizations properties.
    // Ensure that a value is set for the resource's snippet.defaultLanguage property.
    // To set the snippet.defaultLanguage property for a channel resource,
    // you actually need to update the brandingSettings.channel.defaultLanguage property.
    channel.getBrandingSettings().getChannel().setDefaultLanguage(defaultLanguage);
    // Preserve any localizations already associated with the channel. If the
    // channel does not have any localizations, create a new array. Append the
    // provided localization to the list of localizations associated with the channel.
    Map<String, ChannelLocalization> localizations = channel.getLocalizations();
    if (localizations == null) {
        localizations = new ArrayMap<String, ChannelLocalization>();
        channel.setLocalizations(localizations);
    }
    ChannelLocalization channelLocalization = new ChannelLocalization();
    channelLocalization.setDescription(description);
    localizations.put(language, channelLocalization);
    // Update the channel resource by calling the channels.update() method.
    Channel channelResponse = youtube.channels().update("brandingSettings,localizations", channel).execute();
    // Print information from the API response.
    System.out.println("\n================== Updated Channel ==================\n");
    System.out.println("  - ID: " + channelResponse.getId());
    System.out.println("  - Default Language: " + channelResponse.getSnippet().getDefaultLanguage());
    System.out.println("  - Description(" + language + "): " + channelResponse.getLocalizations().get(language).getDescription());
    System.out.println("\n-------------------------------------------------------------\n");
}
Also used : ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse) ChannelLocalization(com.google.api.services.youtube.model.ChannelLocalization) Channel(com.google.api.services.youtube.model.Channel)

Example 3 with Channel

use of com.google.api.services.youtube.model.Channel in project api-samples by youtube.

the class YouTubeAnalyticsReports method main.

/**
     * This code authorizes the user, uses the YouTube Data API to retrieve
     * information about the user's YouTube channel, and then fetches and
     * prints statistics for the user's channel using the YouTube Analytics API.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // These scopes are required to access information about the
    // authenticated user's YouTube channel as well as Analytics
    // data for that channel.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics.readonly", "https://www.googleapis.com/auth/youtube.readonly");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "analyticsreports");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("youtube-analytics-api-report-example").build();
        // This object is used to make YouTube Analytics API requests.
        analytics = new YouTubeAnalytics.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName("youtube-analytics-api-report-example").build();
        // Construct a request to retrieve the current user's channel ID.
        YouTube.Channels.List channelRequest = youtube.channels().list("id,snippet");
        channelRequest.setMine(true);
        channelRequest.setFields("items(id,snippet/title)");
        ChannelListResponse channels = channelRequest.execute();
        // List channels associated with the user.
        List<Channel> listOfChannels = channels.getItems();
        // The user's default channel is the first item in the list.
        Channel defaultChannel = listOfChannels.get(0);
        String channelId = defaultChannel.getId();
        PrintStream writer = System.out;
        if (channelId == null) {
            writer.println("No channel found.");
        } else {
            writer.println("Default Channel: " + defaultChannel.getSnippet().getTitle() + " ( " + channelId + " )\n");
            printData(writer, "Views Over Time.", executeViewsOverTimeQuery(analytics, channelId));
            printData(writer, "Top Videos", executeTopVideosQuery(analytics, channelId));
            printData(writer, "Demographics", executeDemographicsQuery(analytics, channelId));
        }
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) Credential(com.google.api.client.auth.oauth2.Credential) Channel(com.google.api.services.youtube.model.Channel) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) YouTubeAnalytics(com.google.api.services.youtubeAnalytics.YouTubeAnalytics) ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse)

Example 4 with Channel

use of com.google.api.services.youtube.model.Channel in project api-samples by youtube.

the class MyUploads method main.

/**
     * 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 = "";
            // 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();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) Channel(com.google.api.services.youtube.model.Channel) ArrayList(java.util.ArrayList) YouTube(com.google.api.services.youtube.YouTube) PlaylistItemListResponse(com.google.api.services.youtube.model.PlaylistItemListResponse) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse) PlaylistItem(com.google.api.services.youtube.model.PlaylistItem)

Example 5 with Channel

use of com.google.api.services.youtube.model.Channel in project api-samples by youtube.

the class ChannelLocalizations method getChannelLocalization.

/**
     * Returns localized metadata for a channel in a selected language.
     * If the localized text is not available in the requested language,
     * this method will return text in the default language.
     *
     * @param channelId The id parameter specifies the channel ID for the resource
     * that is being updated.
     * @param language The language of the localized metadata
     * @throws IOException
     */
private static void getChannelLocalization(String channelId, String language) throws IOException {
    // Call the YouTube Data API's channels.list method to retrieve channels.
    ChannelListResponse channelListResponse = youtube.channels().list("snippet").setId(channelId).set("hl", language).execute();
    // Since the API request specified a unique channel ID, the API
    // response should return exactly one channel. If the response does
    // not contain a channel, then the specified channel ID was not found.
    List<Channel> channelList = channelListResponse.getItems();
    if (channelList.isEmpty()) {
        System.out.println("Can't find a channel with ID: " + channelId);
        return;
    }
    Channel channel = channelList.get(0);
    // Print information from the API response.
    System.out.println("\n================== Channel ==================\n");
    System.out.println("  - ID: " + channel.getId());
    System.out.println("  - Description(" + language + "): " + channel.getLocalizations().get(language).getDescription());
    System.out.println("\n-------------------------------------------------------------\n");
}
Also used : ChannelListResponse(com.google.api.services.youtube.model.ChannelListResponse) Channel(com.google.api.services.youtube.model.Channel)

Aggregations

Channel (com.google.api.services.youtube.model.Channel)5 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)5 Credential (com.google.api.client.auth.oauth2.Credential)2 YouTube (com.google.api.services.youtube.YouTube)2 ChannelLocalization (com.google.api.services.youtube.model.ChannelLocalization)2 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 PlaylistItem (com.google.api.services.youtube.model.PlaylistItem)1 PlaylistItemListResponse (com.google.api.services.youtube.model.PlaylistItemListResponse)1 YouTubeAnalytics (com.google.api.services.youtubeAnalytics.YouTubeAnalytics)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 ArrayList (java.util.ArrayList)1