Search in sources :

Example 16 with YouTube

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

the class PlaylistLocalizations method main.

/**
     * Set and retrieve localized metadata for a playlist.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "localizations");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-localizations-sample").build();
        // Prompt the user to specify the action of the be achieved.
        String actionString = getActionFromUser();
        System.out.println("You chose " + actionString + ".");
        //Map the user input to the enum values.
        Action action = Action.valueOf(actionString.toUpperCase());
        switch(action) {
            case SET:
                setPlaylistLocalization(getId("playlist"), getDefaultLanguage(), getLanguage(), getMetadata("title"), getMetadata("description"));
                break;
            case GET:
                getPlaylistLocalization(getId("playlist"), getLanguage());
                break;
            case LIST:
                listPlaylistLocalizations(getId("playlist"));
                break;
        }
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube)

Example 17 with YouTube

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

the class PlaylistUpdates method main.

/**
     * Authorize the user, create a playlist, and add an item to the playlist.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "playlistupdates");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-playlistupdates-sample").build();
        // Create a new, private playlist in the authorized user's channel.
        String playlistId = insertPlaylist();
        // If a valid playlist was created, add a video to that playlist.
        insertPlaylistItem(playlistId, VIDEO_ID);
    } catch (GoogleJsonResponseException e) {
        System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube)

Example 18 with YouTube

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

the class Search method main.

/**
     * Initialize a YouTube object to search for videos on YouTube. Then
     * display the name and thumbnail image of each video in the result set.
     *
     * @param args command line args.
     */
public static void main(String[] args) {
    // Read the developer key from the properties file.
    Properties properties = new Properties();
    try {
        InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
        properties.load(in);
    } catch (IOException e) {
        System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause() + " : " + e.getMessage());
        System.exit(1);
    }
    try {
        // This object is used to make YouTube Data API requests. The last
        // argument is required, but since we don't need anything
        // initialized when the HttpRequest is initialized, we override
        // the interface and provide a no-op function.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, new HttpRequestInitializer() {

            public void initialize(HttpRequest request) throws IOException {
            }
        }).setApplicationName("youtube-cmdline-search-sample").build();
        // Prompt the user to enter a query term.
        String queryTerm = getInputQuery();
        // Define the API request for retrieving search results.
        YouTube.Search.List search = youtube.search().list("id,snippet");
        // Set your developer key from the {{ Google Cloud Console }} for
        // non-authenticated requests. See:
        // {{ https://cloud.google.com/console }}
        String apiKey = properties.getProperty("youtube.apikey");
        search.setKey(apiKey);
        search.setQ(queryTerm);
        // Restrict the search results to only include videos. See:
        // https://developers.google.com/youtube/v3/docs/search/list#type
        search.setType("video");
        // To increase efficiency, only retrieve the fields that the
        // application uses.
        search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
        search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
        // Call the API and print results.
        SearchListResponse searchResponse = search.execute();
        List<SearchResult> searchResultList = searchResponse.getItems();
        if (searchResultList != null) {
            prettyPrint(searchResultList.iterator(), queryTerm);
        }
    } 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();
    }
}
Also used : HttpRequest(com.google.api.client.http.HttpRequest) SearchListResponse(com.google.api.services.youtube.model.SearchListResponse) InputStream(java.io.InputStream) SearchResult(com.google.api.services.youtube.model.SearchResult) IOException(java.io.IOException) Properties(java.util.Properties) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) HttpRequestInitializer(com.google.api.client.http.HttpRequestInitializer)

Example 19 with YouTube

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

the class ChannelBulletin method main.

/**
     * Authorize the user, call the youtube.channels.list method to retrieve
     * information about the user's YouTube channel, and post a bulletin with
     * a video ID to that channel.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "channelbulletin");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-channelbulletin-sample").build();
        // Construct a request to retrieve the current user's channel ID.
        // See https://developers.google.com/youtube/v3/docs/channels/list
        YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
        channelRequest.setMine(true);
        // In the API response, only include channel information needed
        // for this use case.
        channelRequest.setFields("items/contentDetails");
        ChannelListResponse channelResult = channelRequest.execute();
        List<Channel> channelsList = channelResult.getItems();
        if (channelsList != null) {
            // The user's default channel is the first item in the list.
            String channelId = channelsList.get(0).getId();
            // Create the snippet for the activity resource that
            // represents the channel bulletin. Set its channel ID
            // and description.
            ActivitySnippet snippet = new ActivitySnippet();
            snippet.setChannelId(channelId);
            Calendar cal = Calendar.getInstance();
            snippet.setDescription("Bulletin test video via YouTube API on " + cal.getTime());
            // Create a resourceId that identifies the video ID. You could
            // set the kind to "youtube#playlist" and use a playlist ID
            // instead of a video ID.
            ResourceId resource = new ResourceId();
            resource.setKind("youtube#video");
            resource.setVideoId(VIDEO_ID);
            ActivityContentDetailsBulletin bulletin = new ActivityContentDetailsBulletin();
            bulletin.setResourceId(resource);
            // Construct the ActivityContentDetails object for the request.
            ActivityContentDetails contentDetails = new ActivityContentDetails();
            contentDetails.setBulletin(bulletin);
            // Construct the resource, including the snippet and content
            // details, to send in the activities.insert
            Activity activity = new Activity();
            activity.setSnippet(snippet);
            activity.setContentDetails(contentDetails);
            // The API request identifies the resource parts that are being
            // written (contentDetails and snippet). The API response will
            // also include those parts.
            YouTube.Activities.Insert insertActivities = youtube.activities().insert("contentDetails,snippet", activity);
            // Return the newly created activity resource.
            Activity newActivityInserted = insertActivities.execute();
            if (newActivityInserted != null) {
                System.out.println("New Activity inserted of type " + newActivityInserted.getSnippet().getType());
                System.out.println(" - Video id " + newActivityInserted.getContentDetails().getBulletin().getResourceId().getVideoId());
                System.out.println(" - Description: " + newActivityInserted.getSnippet().getDescription());
                System.out.println(" - Posted on " + newActivityInserted.getSnippet().getPublishedAt());
            } else {
                System.out.println("Activity failed.");
            }
        } else {
            System.out.println("No channels are assigned to this user.");
        }
    } 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) Calendar(java.util.Calendar) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 20 with YouTube

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

the class ChannelLocalizations method main.

/**
     * Set and retrieve localized metadata for a channel.
     *
     * @param args command line args (not used).
     */
public static void main(String[] args) {
    // This OAuth 2.0 access scope allows for full read/write access to the
    // authenticated user's account.
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "localizations");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-localizations-sample").build();
        // Prompt the user to specify the action of the be achieved.
        String actionString = getActionFromUser();
        System.out.println("You chose " + actionString + ".");
        //Map the user input to the enum values.
        Action action = Action.valueOf(actionString.toUpperCase());
        switch(action) {
            case SET:
                setChannelLocalization(getId("channel"), getDefaultLanguage(), getLanguage(), getMetadata("description"));
                break;
            case GET:
                getChannelLocalization(getId("channel"), getLanguage());
                break;
            case LIST:
                listChannelLocalizations(getId("channel"));
                break;
        }
    } catch (GoogleJsonResponseException e) {
        System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("IOException: " + e.getMessage());
        e.printStackTrace();
    } catch (Throwable t) {
        System.err.println("Throwable: " + t.getMessage());
        t.printStackTrace();
    }
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube)

Aggregations

YouTube (com.google.api.services.youtube.YouTube)22 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)21 Credential (com.google.api.client.auth.oauth2.Credential)20 IOException (java.io.IOException)19 InputStreamContent (com.google.api.client.http.InputStreamContent)4 Video (com.google.api.services.youtube.model.Video)4 ArrayList (java.util.ArrayList)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3 VideoSnippet (com.google.api.services.youtube.model.VideoSnippet)3 Calendar (java.util.Calendar)3 HttpRequest (com.google.api.client.http.HttpRequest)2 HttpRequestInitializer (com.google.api.client.http.HttpRequestInitializer)2 Channel (com.google.api.services.youtube.model.Channel)2 ChannelListResponse (com.google.api.services.youtube.model.ChannelListResponse)2 Comment (com.google.api.services.youtube.model.Comment)2 CommentSnippet (com.google.api.services.youtube.model.CommentSnippet)2 CommentThread (com.google.api.services.youtube.model.CommentThread)2 CommentThreadListResponse (com.google.api.services.youtube.model.CommentThreadListResponse)2 SearchListResponse (com.google.api.services.youtube.model.SearchListResponse)2