|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.services.samples.youtube.cmdline.data; |
| 16 | + |
| 17 | +import com.google.api.client.auth.oauth2.Credential; |
| 18 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
| 19 | +import com.google.api.services.samples.youtube.cmdline.Auth; |
| 20 | +import com.google.api.services.youtube.YouTube; |
| 21 | +import com.google.api.services.youtube.model.ResourceId; |
| 22 | +import com.google.api.services.youtube.model.Subscription; |
| 23 | +import com.google.api.services.youtube.model.SubscriptionSnippet; |
| 24 | +import com.google.common.collect.Lists; |
| 25 | + |
| 26 | +import java.io.BufferedReader; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStreamReader; |
| 29 | +import java.util.List; |
| 30 | + |
| 31 | +/** |
| 32 | + * Subscribe a user to a channel using the YouTube Data API (v3). Use |
| 33 | + * OAuth 2.0 for authorization. |
| 34 | + * |
| 35 | + * @author Ibrahim Ulukaya |
| 36 | + */ |
| 37 | +public class AddSubscription { |
| 38 | + |
| 39 | + /** |
| 40 | + * Define a global instance of a Youtube object, which will be used |
| 41 | + * to make YouTube Data API requests. |
| 42 | + */ |
| 43 | + private static YouTube youtube; |
| 44 | + |
| 45 | + /** |
| 46 | + * Subscribe the user's YouTube account to a user-selected channel. |
| 47 | + * |
| 48 | + * @param args command line args (not used). |
| 49 | + */ |
| 50 | + public static void main(String[] args) { |
| 51 | + |
| 52 | + // This OAuth 2.0 access scope allows for full read/write access to the |
| 53 | + // authenticated user's account. |
| 54 | + List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube"); |
| 55 | + |
| 56 | + try { |
| 57 | + // Authorize the request. |
| 58 | + Credential credential = Auth.authorize(scopes, "addsubscription"); |
| 59 | + |
| 60 | + // This object is used to make YouTube Data API requests. |
| 61 | + youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName( |
| 62 | + "youtube-cmdline-addsubscription-sample").build(); |
| 63 | + |
| 64 | + // We get the user selected channel to subscribe. |
| 65 | + // Retrieve the channel ID that the user is subscribing to. |
| 66 | + String channelId = getChannelId(); |
| 67 | + System.out.println("You chose " + channelId + " to subscribe."); |
| 68 | + |
| 69 | + // Create a resourceId that identifies the channel ID. |
| 70 | + ResourceId resourceId = new ResourceId(); |
| 71 | + resourceId.setChannelId(channelId); |
| 72 | + resourceId.setKind("youtube#channel"); |
| 73 | + |
| 74 | + // Create a snippet that contains the resourceId. |
| 75 | + SubscriptionSnippet snippet = new SubscriptionSnippet(); |
| 76 | + snippet.setResourceId(resourceId); |
| 77 | + |
| 78 | + // Create a request to add the subscription and send the request. |
| 79 | + // The request identifies subscription metadata to insert as well |
| 80 | + // as information that the API server should return in its response. |
| 81 | + Subscription subscription = new Subscription(); |
| 82 | + subscription.setSnippet(snippet); |
| 83 | + YouTube.Subscriptions.Insert subscriptionInsert = |
| 84 | + youtube.subscriptions().insert("snippet,contentDetails", subscription); |
| 85 | + Subscription returnedSubscription = subscriptionInsert.execute(); |
| 86 | + |
| 87 | + // Print information from the API response. |
| 88 | + System.out.println("\n================== Returned Subscription ==================\n"); |
| 89 | + System.out.println(" - Id: " + returnedSubscription.getId()); |
| 90 | + System.out.println(" - Title: " + returnedSubscription.getSnippet().getTitle()); |
| 91 | + |
| 92 | + } catch (GoogleJsonResponseException e) { |
| 93 | + System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " |
| 94 | + + e.getDetails().getMessage()); |
| 95 | + e.printStackTrace(); |
| 96 | + |
| 97 | + } catch (IOException e) { |
| 98 | + System.err.println("IOException: " + e.getMessage()); |
| 99 | + e.printStackTrace(); |
| 100 | + } catch (Throwable t) { |
| 101 | + System.err.println("Throwable: " + t.getMessage()); |
| 102 | + t.printStackTrace(); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /* |
| 107 | + * Prompt the user to enter a channel ID and return it. |
| 108 | + */ |
| 109 | + private static String getChannelId() throws IOException { |
| 110 | + |
| 111 | + String channelId = ""; |
| 112 | + |
| 113 | + System.out.print("Please enter a channel id: "); |
| 114 | + BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); |
| 115 | + channelId = bReader.readLine(); |
| 116 | + |
| 117 | + if (channelId.length() < 1) { |
| 118 | + // If nothing is entered, defaults to "YouTube For Developers." |
| 119 | + channelId = "UCtVd0c0tGXuTSbU5d8cSBUg"; |
| 120 | + } |
| 121 | + return channelId; |
| 122 | + } |
| 123 | +} |
0 commit comments