use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class AddSubscription method main.
/**
* Subscribe the user's YouTube account to a user-selected 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, "addsubscription");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-addsubscription-sample").build();
// We get the user selected channel to subscribe.
// Retrieve the channel ID that the user is subscribing to.
String channelId = getChannelId();
System.out.println("You chose " + channelId + " to subscribe.");
// Create a resourceId that identifies the channel ID.
ResourceId resourceId = new ResourceId();
resourceId.setChannelId(channelId);
resourceId.setKind("youtube#channel");
// Create a snippet that contains the resourceId.
SubscriptionSnippet snippet = new SubscriptionSnippet();
snippet.setResourceId(resourceId);
// Create a request to add the subscription and send the request.
// The request identifies subscription metadata to insert as well
// as information that the API server should return in its response.
Subscription subscription = new Subscription();
subscription.setSnippet(snippet);
YouTube.Subscriptions.Insert subscriptionInsert = youtube.subscriptions().insert("snippet,contentDetails", subscription);
Subscription returnedSubscription = subscriptionInsert.execute();
// Print information from the API response.
System.out.println("\n================== Returned Subscription ==================\n");
System.out.println(" - Id: " + returnedSubscription.getId());
System.out.println(" - Title: " + returnedSubscription.getSnippet().getTitle());
} 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class CommentThreads method main.
/**
* Create, list and update top-level channel and video comments.
*
* @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 and requires requests to use an SSL connection.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "commentthreads");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-commentthreads-sample").build();
// Prompt the user for the ID of a channel to comment on.
// Retrieve the channel ID that the user is commenting to.
String channelId = getChannelId();
System.out.println("You chose " + channelId + " to subscribe.");
// Prompt the user for the ID of a video to comment on.
// Retrieve the video ID that the user is commenting to.
String videoId = getVideoId();
System.out.println("You chose " + videoId + " to subscribe.");
// Prompt the user for the comment text.
// Retrieve the text that the user is commenting.
String text = getText();
System.out.println("You chose " + text + " to subscribe.");
// Insert channel comment by omitting videoId.
// Create a comment snippet with text.
CommentSnippet commentSnippet = new CommentSnippet();
commentSnippet.setTextOriginal(text);
// Create a top-level comment with snippet.
Comment topLevelComment = new Comment();
topLevelComment.setSnippet(commentSnippet);
// Create a comment thread snippet with channelId and top-level
// comment.
CommentThreadSnippet commentThreadSnippet = new CommentThreadSnippet();
commentThreadSnippet.setChannelId(channelId);
commentThreadSnippet.setTopLevelComment(topLevelComment);
// Create a comment thread with snippet.
CommentThread commentThread = new CommentThread();
commentThread.setSnippet(commentThreadSnippet);
// Call the YouTube Data API's commentThreads.insert method to
// create a comment.
CommentThread channelCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
// Print information from the API response.
System.out.println("\n================== Created Channel Comment ==================\n");
CommentSnippet snippet = channelCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
// Insert video comment
commentThreadSnippet.setVideoId(videoId);
// Call the YouTube Data API's commentThreads.insert method to
// create a comment.
CommentThread videoCommentInsertResponse = youtube.commentThreads().insert("snippet", commentThread).execute();
// Print information from the API response.
System.out.println("\n================== Created Video Comment ==================\n");
snippet = videoCommentInsertResponse.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
// Call the YouTube Data API's commentThreads.list method to
// retrieve video comment threads.
CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads().list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();
List<CommentThread> videoComments = videoCommentsListResponse.getItems();
if (videoComments.isEmpty()) {
System.out.println("Can't get video comments.");
} else {
// Print information from the API response.
System.out.println("\n================== Returned Video Comments ==================\n");
for (CommentThread videoComment : videoComments) {
snippet = videoComment.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
CommentThread firstComment = videoComments.get(0);
firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
CommentThread videoCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
// Print information from the API response.
System.out.println("\n================== Updated Video Comment ==================\n");
snippet = videoCommentUpdateResponse.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
// Call the YouTube Data API's commentThreads.list method to
// retrieve channel comment threads.
CommentThreadListResponse channelCommentsListResponse = youtube.commentThreads().list("snippet").setChannelId(channelId).setTextFormat("plainText").execute();
List<CommentThread> channelComments = channelCommentsListResponse.getItems();
if (channelComments.isEmpty()) {
System.out.println("Can't get channel comments.");
} else {
// Print information from the API response.
System.out.println("\n================== Returned Channel Comments ==================\n");
for (CommentThread channelComment : channelComments) {
snippet = channelComment.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
CommentThread firstComment = channelComments.get(0);
firstComment.getSnippet().getTopLevelComment().getSnippet().setTextOriginal("updated");
CommentThread channelCommentUpdateResponse = youtube.commentThreads().update("snippet", firstComment).execute();
// Print information from the API response.
System.out.println("\n================== Updated Channel Comment ==================\n");
snippet = channelCommentUpdateResponse.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}
} 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException in project api-samples by youtube.
the class Captions method main.
/**
* Upload, list, update, download, and delete caption tracks.
*
* @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 and requires requests to use an SSL connection.
List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.force-ssl");
try {
// Authorize the request.
Credential credential = Auth.authorize(scopes, "captions");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-captions-sample").build();
// Prompt the user to specify the action of the be achieved.
String actionString = getActionFromUser();
System.out.println("You chose " + actionString + ".");
Action action = Action.valueOf(actionString.toUpperCase());
switch(action) {
case UPLOAD:
uploadCaption(getVideoId(), getLanguage(), getName(), getCaptionFromUser());
break;
case LIST:
listCaptions(getVideoId());
break;
case UPDATE:
updateCaption(getCaptionIDFromUser(), getUpdateCaptionFromUser());
break;
case DOWNLOAD:
downloadCaption(getCaptionIDFromUser());
break;
case DELETE:
deleteCaption(getCaptionIDFromUser());
break;
default:
// All the available methods are used in sequence just for the sake
// of an example.
// Prompt the user to specify a video to upload the caption track for and
// a language, a name, a binary file for the caption track. Then upload the
// caption track with the values that are selected by the user.
String videoId = getVideoId();
uploadCaption(videoId, getLanguage(), getName(), getCaptionFromUser());
List<Caption> captions = listCaptions(videoId);
if (captions.isEmpty()) {
System.out.println("Can't get video caption tracks.");
} else {
// Retrieve the first uploaded caption track.
String firstCaptionId = captions.get(0).getId();
updateCaption(firstCaptionId, null);
downloadCaption(firstCaptionId);
deleteCaption(firstCaptionId);
}
}
} 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();
}
}
use of com.google.api.client.googleapis.json.GoogleJsonResponseException 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();
}
}
Aggregations