Search in sources :

Example 86 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.

the class RetrieveReports method main.

/**
 * Retrieve reports.
 *
 * @param args command line args (not used).
 */
public static void main(String[] args) {
    /*
         * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for
         * authenticated user's account. Any request that retrieves earnings or ad performance metrics must
         * use this scope.
         */
    List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics-monetary.readonly");
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "retrievereports");
        // This object is used to make YouTube Reporting API requests.
        youtubeReporting = new YouTubeReporting.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-retrievereports-sample").build();
        if (listReportingJobs()) {
            if (retrieveReports(getJobIdFromUser())) {
                downloadReport(getReportUrlFromUser());
            }
        }
    } 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) YouTubeReporting(com.google.api.services.youtubereporting.YouTubeReporting) IOException(java.io.IOException)

Example 87 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.

the class InvideoProgramming method main.

/**
 * This code sample demonstrates different ways that the API can be used to
 * promote your channel content. It includes code for the following tasks:
 * <ol>
 * <li>Feature a video.</li>
 * <li>Feature a link to a social media channel.</li>
 * <li>Set a watermark for videos on your channel.</li>
 * </ol>
 *
 * @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, "invideoprogramming");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-invideoprogramming-sample").build();
        // Construct a request to retrieve the current user's channel ID.
        // In the API response, only include channel information needed for
        // this use case. The channel's uploads playlist identifies the
        // channel's most recently uploaded video.
        // See https://developers.google.com/youtube/v3/docs/channels/list
        ChannelListResponse channelListResponse = youtube.channels().list("id,contentDetails").setMine(true).setFields("items(contentDetails/relatedPlaylists/uploads,id)").execute();
        // The user's default channel is the first item in the list. If the
        // user does not have a channel, this code should throw a
        // GoogleJsonResponseException explaining the issue.
        Channel myChannel = channelListResponse.getItems().get(0);
        String channelId = myChannel.getId();
        // The promotion appears 15000ms (15 seconds) before the video ends.
        InvideoTiming invideoTiming = new InvideoTiming();
        invideoTiming.setOffsetMs(BigInteger.valueOf(15000l));
        invideoTiming.setType("offsetFromEnd");
        // This is one type of promotion and promotes a video.
        PromotedItemId promotedItemId = new PromotedItemId();
        promotedItemId.setType("video");
        promotedItemId.setVideoId(FEATURED_VIDEO_ID);
        // Set a custom message providing additional information about the
        // promoted video or your channel.
        PromotedItem promotedItem = new PromotedItem();
        promotedItem.setCustomMessage("Check out this video about WebM!");
        promotedItem.setId(promotedItemId);
        // Construct an object representing the invideo promotion data, and
        // add it to the channel.
        InvideoPromotion invideoPromotion = new InvideoPromotion();
        invideoPromotion.setDefaultTiming(invideoTiming);
        invideoPromotion.setItems(Lists.newArrayList(promotedItem));
        Channel channel = new Channel();
        channel.setId(channelId);
        channel.setInvideoPromotion(invideoPromotion);
        Channel updateChannelResponse = youtube.channels().update("invideoPromotion", channel).execute();
        // Print data from the API response.
        System.out.println("\n================== Updated Channel Information ==================\n");
        System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
        InvideoPromotion promotions = updateChannelResponse.getInvideoPromotion();
        // We only care about the first item
        promotedItem = promotions.getItems().get(0);
        System.out.println("\t- Invideo promotion video ID: " + promotedItem.getId().getVideoId());
        System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage());
        // In-video programming can also be used to feature links to
        // associated websites, merchant sites, or social networking sites.
        // The code below overrides the promotional video set above by
        // featuring a link to the YouTube Developers Twitter feed.
        PromotedItemId promotedTwitterFeed = new PromotedItemId();
        promotedTwitterFeed.setType("website");
        promotedTwitterFeed.setWebsiteUrl("https://twitter.com/youtubedev");
        promotedItem = new PromotedItem();
        promotedItem.setCustomMessage("Follow us on Twitter!");
        promotedItem.setId(promotedTwitterFeed);
        invideoPromotion.setItems(Lists.newArrayList(promotedItem));
        channel.setInvideoPromotion(invideoPromotion);
        // Call the API to set the in-video promotion data.
        updateChannelResponse = youtube.channels().update("invideoPromotion", channel).execute();
        // Print data from the API response.
        System.out.println("\n================== Updated Channel Information ==================\n");
        System.out.println("\t- Channel ID: " + updateChannelResponse.getId());
        promotions = updateChannelResponse.getInvideoPromotion();
        promotedItem = promotions.getItems().get(0);
        System.out.println("\t- Invideo promotion URL: " + promotedItem.getId().getWebsiteUrl());
        System.out.println("\t- Promotion message: " + promotedItem.getCustomMessage());
        // This example sets a custom watermark for the channel. The image
        // used is the watermark.jpg file in the "resources/" directory.
        InputStreamContent mediaContent = new InputStreamContent("image/jpeg", InvideoProgramming.class.getResourceAsStream("/watermark.jpg"));
        // Indicate that the watermark should display during the last 15
        // seconds of the video.
        InvideoTiming watermarkTiming = new InvideoTiming();
        watermarkTiming.setType("offsetFromEnd");
        watermarkTiming.setDurationMs(BigInteger.valueOf(15000l));
        watermarkTiming.setOffsetMs(BigInteger.valueOf(15000l));
        InvideoBranding invideoBranding = new InvideoBranding();
        invideoBranding.setTiming(watermarkTiming);
        youtube.watermarks().set(channelId, invideoBranding, mediaContent).execute();
    } 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();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) InputStreamContent(com.google.api.client.http.InputStreamContent)

Example 88 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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 89 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.

the class CreateBroadcast method main.

/**
 * Create and insert a liveBroadcast resource.
 */
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, "createbroadcast");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-createbroadcast-sample").build();
        // Prompt the user to enter a title for the broadcast.
        String title = getBroadcastTitle();
        System.out.println("You chose " + title + " for broadcast title.");
        // Create a snippet with the title and scheduled start and end
        // times for the broadcast. Currently, those times are hard-coded.
        LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
        broadcastSnippet.setTitle(title);
        broadcastSnippet.setScheduledStartTime(new DateTime("2024-01-30T00:00:00.000Z"));
        broadcastSnippet.setScheduledEndTime(new DateTime("2024-01-31T00:00:00.000Z"));
        // Set the broadcast's privacy status to "private". See:
        // https://developers.google.com/youtube/v3/live/docs/liveBroadcasts#status.privacyStatus
        LiveBroadcastStatus status = new LiveBroadcastStatus();
        status.setPrivacyStatus("private");
        LiveBroadcast broadcast = new LiveBroadcast();
        broadcast.setKind("youtube#liveBroadcast");
        broadcast.setSnippet(broadcastSnippet);
        broadcast.setStatus(status);
        // Construct and execute the API request to insert the broadcast.
        YouTube.LiveBroadcasts.Insert liveBroadcastInsert = youtube.liveBroadcasts().insert("snippet,status", broadcast);
        LiveBroadcast returnedBroadcast = liveBroadcastInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Broadcast ==================\n");
        System.out.println("  - Id: " + returnedBroadcast.getId());
        System.out.println("  - Title: " + returnedBroadcast.getSnippet().getTitle());
        System.out.println("  - Description: " + returnedBroadcast.getSnippet().getDescription());
        System.out.println("  - Published At: " + returnedBroadcast.getSnippet().getPublishedAt());
        System.out.println("  - Scheduled Start Time: " + returnedBroadcast.getSnippet().getScheduledStartTime());
        System.out.println("  - Scheduled End Time: " + returnedBroadcast.getSnippet().getScheduledEndTime());
        // Prompt the user to enter a title for the video stream.
        title = getStreamTitle();
        System.out.println("You chose " + title + " for stream title.");
        // Create a snippet with the video stream's title.
        LiveStreamSnippet streamSnippet = new LiveStreamSnippet();
        streamSnippet.setTitle(title);
        // Define the content distribution network settings for the
        // video stream. The settings specify the stream's format and
        // ingestion type. See:
        // https://developers.google.com/youtube/v3/live/docs/liveStreams#cdn
        CdnSettings cdnSettings = new CdnSettings();
        cdnSettings.setFormat("1080p");
        cdnSettings.setIngestionType("rtmp");
        LiveStream stream = new LiveStream();
        stream.setKind("youtube#liveStream");
        stream.setSnippet(streamSnippet);
        stream.setCdn(cdnSettings);
        // Construct and execute the API request to insert the stream.
        YouTube.LiveStreams.Insert liveStreamInsert = youtube.liveStreams().insert("snippet,cdn", stream);
        LiveStream returnedStream = liveStreamInsert.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Stream ==================\n");
        System.out.println("  - Id: " + returnedStream.getId());
        System.out.println("  - Title: " + returnedStream.getSnippet().getTitle());
        System.out.println("  - Description: " + returnedStream.getSnippet().getDescription());
        System.out.println("  - Published At: " + returnedStream.getSnippet().getPublishedAt());
        // Construct and execute a request to bind the new broadcast
        // and stream.
        YouTube.LiveBroadcasts.Bind liveBroadcastBind = youtube.liveBroadcasts().bind(returnedBroadcast.getId(), "id,contentDetails");
        liveBroadcastBind.setStreamId(returnedStream.getId());
        returnedBroadcast = liveBroadcastBind.execute();
        // Print information from the API response.
        System.out.println("\n================== Returned Bound Broadcast ==================\n");
        System.out.println("  - Broadcast Id: " + returnedBroadcast.getId());
        System.out.println("  - Bound Stream Id: " + returnedBroadcast.getContentDetails().getBoundStreamId());
    } 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 : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) DateTime(com.google.api.client.util.DateTime) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException)

Example 90 with GoogleJsonResponseException

use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.

the class GetLiveChatId method main.

/**
 * Poll live chat messages and SuperChat details from a live broadcast.
 *
 * @param args videoId (optional). If the videoId is given, live chat messages will be retrieved
 * from the chat associated with this video. If the videoId is not specified, the signed in
 * user's current live broadcast will be used instead.
 */
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(YouTubeScopes.YOUTUBE_READONLY);
    try {
        // Authorize the request.
        Credential credential = Auth.authorize(scopes, "getlivechatid");
        // This object is used to make YouTube Data API requests.
        youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-getlivechatid-sample").build();
        // Get the liveChatId
        String liveChatId = args.length == 1 ? getLiveChatId(youtube, args[0]) : getLiveChatId(youtube);
        if (liveChatId != null) {
            System.out.println("Live chat id: " + liveChatId);
        } else {
            System.err.println("Unable to find a live chat id");
            System.exit(1);
        }
    } 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

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)94 IOException (java.io.IOException)48 YouTube (com.google.api.services.youtube.YouTube)26 Credential (com.google.api.client.auth.oauth2.Credential)25 ArrayList (java.util.ArrayList)13 Operation (com.google.api.services.compute.model.Operation)12 Test (org.junit.Test)12 Compute (com.google.api.services.compute.Compute)11 Storage (com.google.api.services.storage.Storage)10 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)8 GcsOptions (org.apache.beam.sdk.extensions.gcp.options.GcsOptions)7 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)5 InputStreamContent (com.google.api.client.http.InputStreamContent)5 BackOff (com.google.api.client.util.BackOff)5 HashMap (java.util.HashMap)5 Objects (com.google.api.services.storage.model.Objects)4 GoogleCloudStorage (com.google.cloud.hadoop.gcsio.GoogleCloudStorage)4 HalException (com.netflix.spinnaker.halyard.core.error.v1.HalException)4 MediaHttpUploader (com.google.api.client.googleapis.media.MediaHttpUploader)3 MediaHttpUploaderProgressListener (com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener)3