Search in sources :

Example 1 with Subscription

use of com.google.api.services.youtube.model.Subscription 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();
    }
}
Also used : Credential(com.google.api.client.auth.oauth2.Credential) IOException(java.io.IOException) YouTube(com.google.api.services.youtube.YouTube) SubscriptionSnippet(com.google.api.services.youtube.model.SubscriptionSnippet) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) ResourceId(com.google.api.services.youtube.model.ResourceId) Subscription(com.google.api.services.youtube.model.Subscription)

Aggregations

Credential (com.google.api.client.auth.oauth2.Credential)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 YouTube (com.google.api.services.youtube.YouTube)1 ResourceId (com.google.api.services.youtube.model.ResourceId)1 Subscription (com.google.api.services.youtube.model.Subscription)1 SubscriptionSnippet (com.google.api.services.youtube.model.SubscriptionSnippet)1 IOException (java.io.IOException)1