Jeetu Alex - IT Software Professional specialized in developing high-end, customized business solutions using Coldfusion and Java.

Coldfusion
Java
JQuery

An online resource for latest web technologies like Coldfusion, JRun, Pro*C, JQuery, HTML5, PHP, W3C, Java, J2EE, C, C++, ORACLE, PL/SQL, MySql, Ajax, Coldbox, Fusebox, UNIX, JavaScript, NodeJS and much more...

Sunday, August 25, 2013

YouTube Data API - Java Code Samples

Check the documentation here


package com.google.api.services.samples.youtube.cmdline.youtube_cmdline_channelbulletin_sample;

import java.io.File;
import java.util.Calendar;
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.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.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.ActivityContentDetails.Bulletin;
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;

/**
 * Creates a video bulletin that is posted to the user's channel feed.
 *
 * @author Jeremy Walker
 */
public class ChannelBulletin {

  /** Global instance of the HTTP transport. */
  private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();

  /** Global instance of the JSON factory. */
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();

  /** Global instance of YouTube object to make all API requests. */
  private static YouTube youtube;

  /*
   * Global instance of the video id we want to post as a bulletin into our channel feed. You will
   * probably pull this from a search or your app.
   */
  private static String VIDEO_ID = "L-oNKK1CrnU";

  /**
   * Authorizes the installed application to access user's protected data.
   *
   * @param scopes list of scopes needed to run upload.
   */
  private static Credential authorize(List scopes) throws Exception {

    // Load client secrets.
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(
        JSON_FACTORY, ChannelBulletin.class.getResourceAsStream("/client_secrets.json"));

    // Checks that the defaults have been replaced (Default = "Enter X here").
    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");
      System.exit(1);
    }

    // Set up file credential store.
    FileCredentialStore credentialStore = new FileCredentialStore(
        new File(System.getProperty("user.home"), ".credentials/youtube-api-channelbulletin.json"),
        JSON_FACTORY);

    // Set up authorization code flow.
    GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, scopes).setCredentialStore(credentialStore)
        .build();

    // Build the local server and bind it to port 8080
    LocalServerReceiver localReceiver = new LocalServerReceiver.Builder().setPort(8080).build();

    // Authorize.
    return new AuthorizationCodeInstalledApp(flow, localReceiver).authorize("user");
  }

  /**
   * Authorizes user, runs Youtube.Channnels.List to get the default channel, and posts a bulletin
   * with a video id to the user's default channel.
   *
   * @param args command line args (not used).
   */
  public static void main(String[] args) {

    // Scope required to upload to YouTube.
    List scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube");

    try {
      // Authorization.
      Credential credential = authorize(scopes);

      // YouTube object used to make all API requests.
      youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential).setApplicationName(
          "youtube-cmdline-channelbulletin-sample").build();

      /*
       * Now that the user is authenticated, the app makes a channel list request to get the
       * authenticated user's channel. https://developers.google.com/youtube/v3/docs/channels/list
       */
      YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
      channelRequest.setMine("true");
      /*
       * Limits the results to only the data we need making your app more efficient.
       */
      channelRequest.setFields("items/contentDetails");
      ChannelListResponse channelResult = channelRequest.execute();

      /*
       * Gets the list of channels associated with the user.
       */
      List channelsList = channelResult.getItems();

      if (channelsList != null) {
        // Gets user's default channel id (first channel in list).
        String channelId = channelsList.get(0).getId();

        /*
         * We create the snippet to set the channel we will post to and the description that goes
         * along with our bulletin.
         */
        ActivitySnippet snippet = new ActivitySnippet();
        snippet.setChannelId(channelId);
        Calendar cal = Calendar.getInstance();
        snippet.setDescription("Bulletin test video via YouTube API on " + cal.getTime());

        /*
         * We set the kind of the ResourceId to video (youtube#video). Please note, you could set
         * the type to a playlist (youtube#playlist) and use a playlist id instead of a video id.
         */
        ResourceId resource = new ResourceId();
        resource.setKind("youtube#video");
        resource.setVideoId(VIDEO_ID);

        Bulletin bulletin = new Bulletin();
        bulletin.setResourceId(resource);

        // We construct the ActivityContentDetails now that we have the Bulletin.
        ActivityContentDetails contentDetails = new ActivityContentDetails();
        contentDetails.setBulletin(bulletin);

        /*
         * Finally, we construct the activity we will write to YouTube via the API. We set the
         * snippet (covers description and channel we are posting to) and the content details
         * (covers video id and type).
         */
        Activity activity = new Activity();
        activity.setSnippet(snippet);
        activity.setContentDetails(contentDetails);

        /*
         * We specify the parts (contentDetails and snippet) we will write to YouTube. Those also
         * cover the parts that are returned.
         */
        YouTube.Activities.Insert insertActivities =
            youtube.activities().insert("contentDetails,snippet", activity);
        // This returns the Activity that was added to the user's YouTube channel.
        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();
    }
  }
}

36 comments :

for IT the said...

Awesome documentation on Java Training in Chennai YouTube Data API - Java Online Course Java Code Samples

Giri Mani 2 said...

Got a creative information. Understand well in this. This gives the easy technique of experiment. New technologies are developed more. so techniques are also improved. Thank you for this information.
Java Training in Chennai

Jeanne Davies said...

Thanks for the information. Helped us to convince most on how this process works and what they could achieve by following these guidelinese commerce website design and development

Arjun Rishi said...

Useful post. Happy to visit your blog. Thanks for sharing.

web design courses in chennai

Anu Sri said...

Wow! great article. Glad to find your blog. Thanks for sharing.

website design training in chennai

Gopi Perumal said...

Amazing article..
SAP Training in Chennai | SAP Training Institutes in Chennai | Best SAP Training in Chennai

Gopi Perumal said...

Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
Regards,

Core JAVA Training in Chennai | JAVA Training in Chennai

Gopi Perumal said...

Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing.
Regards,
PMP Training | PMP Training in Chennai

Ramya Krishnan said...

Useful info and nice article, glad to read and understand about this article.
Java Training in chennai

venkatesh raghunath said...

Interesting and informative article.. very useful to me.. thanks for sharing your wonderful ideas.. please keep on updating.


Software Testing Training in chennai | Android Training in chennai

Swetha U said...

very interesting post.. thanks to sharing me..

PHP Training Institute in Chennai

for IT the said...

Java Training Institutes Java Training Institutes Java EE Training in Chennai Java EE Training in Chennai Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training Java Online Training

Jonathan Bennet said...

Yo, it seems for me that its a little bit complicated solution, especially for beginners. Why do we need do all this things, if we can just use this amazing widget https://elfsight.com/youtube-channel-plugin-yottie/ that will be understandable even for a child?

sai said...

This is my 1st visit to your web... But I'm so impressed with your content. Good Job!
python training in chennai | python training in chennai | python training in bangalore

gkr ragini said...

Really very nice blog information for this one and more technical skills are improve,i like that kind of post.
excel advanced excel training in bangalore | Devops Training in Chennai

simbu said...

Excellant post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
Java training in Bangalore | Java training in Btm layout

Java training in Bangalore |Java training in Rajaji nagar

Java training in Bangalore | Java training in Kalyan nagar

ijaz ahamed said...

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.

AWS Training in Bangalore | Amazon Web Services Training in bangalore , india

AWS Training in pune | Amazon Web Services Training in Pune, india

AWS Training in Chennai|Amazon Web Services Training in Chennai,India


aws online training and certification | amazon web services online training ,india

shalinipriya said...

Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.

Data Science training in kalyan nagar
Data Science training in OMR | Data science training in chennai
Data Science training in chennai | Best Data science Training in Chennai
Data science training in velachery | Data Science Training in Chennai
Data science training in tambaram | Data Science training in Chennai
Data science training in jaya nagar | Data science Training in Bangalore

genga g said...

It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
angularjs Training in bangalore

angularjs Training in bangalore

angularjs online Training

angularjs Training in marathahalli

angularjs interview questions and answers

sandy star said...


Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.

Advanced AWS Online Training | Advanced Online AWS Certification Course - Gangboard
Best AWS Training in Chennai | Amazon Web Services Training Institute in Chennai Velachery, Tambaram, OMR
Advanced AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTMLA ,Marathahalli

Learn Digital said...

Your post is really awesome. Your blog is really helpful for me to develop my skills in a right way. Thanks for sharing this unique information with us.
- Digital marketing courses in Bangalore

anushri v said...

You’ve written a really great article here. Your writing style makes this material easy to understand.. I agree with some of the many points you have made. Thank you for this is real thought-provoking content
python training in rajajinagar
Python training in bangalore
Python training in usa

pooja said...

All the points you described so beautiful. Every time i read your i blog and i am so surprised that how you can write so well.
Selenium training in Chennai | Selenium training institute in Chennai | Selenium course in Chennai

Selenium training in Bangalore | Selenium training institute in Bangalore | Selenium course in Bangalore

Selenium interview questions and answers

Selenium training in Pune | Selenium training institute in Pune | Selenium course in Pune

jeeva said...

Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

best rpa training in chennai
rpa training in chennai
rpa interview questions and answers
automation anywhere interview questions and answers
blueprism interview questions and answers
uipath interview questions and answers
rpa training in bangalore

sheela said...

I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.
Best Devops online Training
Online DevOps Certification Course - Gangboard

nivatha said...

I think you have a long story to share and i am glad after long time finally you cam and shared your experience.
Data Science course in Chennai | Best Data Science course in Chennai
Data science course in bangalore | Best Data Science course in Bangalore
Data science course in pune | Data Science Course institute in Pune
Data science online course | Online Data Science certification course-Gangboard
Data Science Interview questions and answers
Data Science Tutorial

SANDY said...

Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.
aws training in bangalore
RPA Training in bangalore
Python Training in bangalore
Selenium Training in bangalore
Hadoop Training in bangalore

alex said...

Only here are the best winnings Online casinos Gaming casino.

sudha P said...

Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.
Advanced AWS Training in Bangalore | Best Amazon Web Services Training Institute in Bangalore
Advanced AWS Training Institute in Pune | Best Amazon Web Services Training Institute in Pune
Advanced AWS Online Training Institute in india | Best Online AWS Certification Course in india
AWS training in bangalore | Best aws training in bangalore

Gowtham said...

Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article. thank you for sharing such a great blog with us.
microsoft azure training in bangalore
rpa training in bangalore
best rpa training in bangalore
rpa online training

Jayamari R said...

This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
Best Devops online Training
Online DevOps Certification Course - Gangboard

jorick228 said...

Casino is not just a game but a lifestyle, come in, play and be stylish. roulette online The best casino is only on BGAOC and nowhere else.

Mounika said...

Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.
python Course in Pune
python Course institute in Chennai
python Training institute in Bangalore

Riyas Fathin said...

It has been simply incredibly generous with you to provide openly what exactly many individuals would’ve marketed for an eBook to end up making some cash for their end, primarily given that you could have tried it in the event you wanted.
Python Training Institute in Chennai| Best Python Training institute in Chennai
RPA Training in Chennai | Best RPA Training institute in Chennai
DevOps Training in Chennai | Best DevOps Training institute in Chennai
Azure Training in Chennai | Best Azure Training institute in Chennai
Java Training in Chennai | Best Java Training institute in Chennai

Priyanka said...

Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore

Chris Hemsworth said...

The article is so informative. This is more helpful for our
Best online software testing training course institute in chennai with placement
Best selenium testing online course training in chennai
Learn best software testing online certification course class in chennai with placement
Thanks for sharing.

Post a Comment