use of com.google.api.client.auth.oauth2.Credential 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();
}
}
use of com.google.api.client.auth.oauth2.Credential in project api-samples by youtube.
the class ListStreams method main.
/**
* List streams for the user's channel.
*/
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, "liststreams");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-liststreams-sample").build();
// Create a request to list liveStream resources.
YouTube.LiveStreams.List livestreamRequest = youtube.liveStreams().list("id,snippet");
// Modify results to only return the user's streams.
livestreamRequest.setMine(true);
// Execute the API request and return the list of streams.
LiveStreamListResponse returnedListResponse = livestreamRequest.execute();
List<LiveStream> returnedList = returnedListResponse.getItems();
// Print information from the API response.
System.out.println("\n================== Returned Streams ==================\n");
for (LiveStream stream : returnedList) {
System.out.println(" - Id: " + stream.getId());
System.out.println(" - Title: " + stream.getSnippet().getTitle());
System.out.println(" - Description: " + stream.getSnippet().getDescription());
System.out.println(" - Published At: " + stream.getSnippet().getPublishedAt());
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.auth.oauth2.Credential 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();
}
}
use of com.google.api.client.auth.oauth2.Credential in project api-samples by youtube.
the class UploadThumbnail method main.
/**
* Prompt the user to specify a video ID and the path for a thumbnail
* image. Then call the API to set the image as the thumbnail for the video.
*
* @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, "uploadthumbnail");
// This object is used to make YouTube Data API requests.
youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName("youtube-cmdline-uploadthumbnail-sample").build();
// Prompt the user to enter the video ID of the video being updated.
String videoId = getVideoIdFromUser();
System.out.println("You chose " + videoId + " to upload a thumbnail.");
// Prompt the user to specify the location of the thumbnail image.
File imageFile = getImageFromUser();
System.out.println("You chose " + imageFile + " to upload.");
// Create an object that contains the thumbnail image file's
// contents.
InputStreamContent mediaContent = new InputStreamContent(IMAGE_FILE_FORMAT, new BufferedInputStream(new FileInputStream(imageFile)));
mediaContent.setLength(imageFile.length());
// Create an API request that specifies that the mediaContent
// object is the thumbnail of the specified video.
Set thumbnailSet = youtube.thumbnails().set(videoId, mediaContent);
// Set the upload type and add an event listener.
MediaHttpUploader uploader = thumbnailSet.getMediaHttpUploader();
// Indicate whether direct media upload is enabled. A value of
// "True" indicates that direct media upload is enabled and that
// the entire media content will be uploaded in a single request.
// A value of "False," which is the default, indicates that the
// request will use the resumable media upload protocol, which
// supports the ability to resume an upload operation after a
// network interruption or other transmission failure, saving
// time and bandwidth in the event of network failures.
uploader.setDirectUploadEnabled(false);
// Set the upload state for the thumbnail image.
MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
@Override
public void progressChanged(MediaHttpUploader uploader) throws IOException {
switch(uploader.getUploadState()) {
// sent.
case INITIATION_STARTED:
System.out.println("Initiation Started");
break;
// completes.
case INITIATION_COMPLETE:
System.out.println("Initiation Completed");
break;
// uploaded.
case MEDIA_IN_PROGRESS:
System.out.println("Upload in progress");
System.out.println("Upload percentage: " + uploader.getProgress());
break;
// been successfully uploaded.
case MEDIA_COMPLETE:
System.out.println("Upload Completed!");
break;
// not started yet.
case NOT_STARTED:
System.out.println("Upload Not Started!");
break;
}
}
};
uploader.setProgressListener(progressListener);
// Upload the image and set it as the specified video's thumbnail.
ThumbnailSetResponse setResponse = thumbnailSet.execute();
// Print the URL for the updated video's thumbnail image.
System.out.println("\n================== Uploaded Thumbnail ==================\n");
System.out.println(" - Url: " + setResponse.getItems().get(0).getDefault().getUrl());
} 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();
}
}
use of com.google.api.client.auth.oauth2.Credential in project openstack4j by ContainX.
the class KeystoneCredentialServiceTests method credentials_update_Test.
// ------------ Credential Tests ------------
// The following tests are to verify the update() method of the
// PolicyService using HTTP PATCH, which is not supported by betamax.
// Find more tests in KeystonePolicyServiceSpec in core-integration-test
// module.
public void credentials_update_Test() throws Exception {
respondWith(JSON_CREDENTIALS_UPDATE);
Credential updatedCredential = osv3().identity().credentials().update(Builders.credential().id(CREDENTIAL_CRUD_ID).blob(CREDENTIAL_CRUD_BLOB_UPDATE).build());
assertEquals(updatedCredential.getId(), CREDENTIAL_CRUD_ID);
assertEquals(updatedCredential.getBlob(), CREDENTIAL_CRUD_BLOB_UPDATE);
assertEquals(updatedCredential.getUserId(), CREDENTIAL_CRUD_USER_ID);
assertEquals(updatedCredential.getProjectId(), CREDENTIAL_CRUD_PROJECT_ID);
}
Aggregations