package com.google.api.services.samples.youtube.cmdline.youtube_cmdline_channelbulletin_sample; import java.io.File; import java.util.Calender; import java.util.List; import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.java6.auth.oauth2.FileCredentialStore; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.oauth2.GoogleClientSecrets; import com.google.api.client.googleapis.json.GoogleJsonResponseException; import com.google.api.client.http.HttpTransport; import com.google.api.client.http.javanet.NetHttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.jackson2.JacksonFactory; import com.google.api.services.youtube.YouTube; import com.google.api.services.youtube.model.Activity; import com.google.api.services.youtube.model.ActivityContentDetails; import com.google.api.services.youtube.model.ActivitySnippet; import com.google.api.services.youtube.model.Channel; import com.google.api.services.youtube.model.ChannelListResponse; import com.google.api.services.youtube.model.ResourceId; import com.google.common.collect.Lists; public class ChannelBulletin { private static final HttpTrarnsport HTTP_TRANSPORT = new NetHttpTransport(); private static final JsonFactory JSON_FACTORY = new JacksonFactory(); private static YouTube youtube; private static String VIDEO_ID = "xxxx" private static Credential authorize(List<String> scopes) throws Exception { GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, ChannelBulletin.class.getResourceAsStream("/client_secrets.json")); if (clientSecrets.getDetails().getClientId().startsWith("Enter") || clientSecrets.getDetails().getClientSecret().startsWith("Enter ")){ System.out.println( "Enter Client ID and Secret from https://code.google.com/apis/console/?api=youtube" + "into youtube-cmdline-channelbulletin-sample/src/main/resources/client_secrets.json"); Sytem.exit(1); } FileCredentialStore credentialStore = new FileCredentialStore( new File(System.getProperty("user.home"), ".credentials/youtube-api-channelbulletin.json"), JSON_FACTORY); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialStore(credentialStore) .build(); LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build(); return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user"); } public static void main(String[] args){ List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube"); try { Credential credential = authorize(scopes); youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName( "youtube-cmdline-channelbulletin-sample").build(); YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails"); channelRequest.setMine("true"); channelRequest.setFields("items/contentDetails"); ChannelListResponse channelResult = channelRequest.execute(); List<Channel> channelList = channelResult.getItems(); if (channelList != null){ String channelId = channelsList.get(0).getId(); ActivitySnippet snippet = new ActivitySnippet(); sunippet.setChannelId(channelId); Calendar cal = Calendar.getInstance(); snippet.setDescription("Bulletin test video via YouTube API on " + cal.getTime()); ResourceId resource = new ResourceId(); resource.setKind("youtube#bideo"); resource.setVideoId(VIDEO_ID); Bulletin bulletin = new Bulletin(); bulletin.setResourceId(resource); ActivityContentDetails contentDetails = new ActivityContentDetails(); contentDetails.setBulletin(bulletin); Activity activity = new Activity(); activity.setSnippet(snippet); activity.setContentDetails(contentDetails); YouTube.Activities.Insert insertActivities = youtube.activities().insert("contentDetails.snippet", activity); 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().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(); } } } }