|
| 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.live; |
| 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.LiveStream; |
| 22 | +import com.google.api.services.youtube.model.LiveStreamListResponse; |
| 23 | +import com.google.common.collect.Lists; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +/** |
| 29 | + * Retrieve a list of a channel's streams, using OAuth 2.0 to authorize |
| 30 | + * API requests. |
| 31 | + * |
| 32 | + * @author Ibrahim Ulukaya |
| 33 | + */ |
| 34 | +public class ListStreams { |
| 35 | + |
| 36 | + /** |
| 37 | + * Define a global instance of a Youtube object, which will be used |
| 38 | + * to make YouTube Data API requests. |
| 39 | + */ |
| 40 | + private static YouTube youtube; |
| 41 | + |
| 42 | + /** |
| 43 | + * List streams for the user's channel. |
| 44 | + */ |
| 45 | + public static void main(String[] args) { |
| 46 | + |
| 47 | + // This OAuth 2.0 access scope allows for read-only access to the |
| 48 | + // authenticated user's account, but not other types of account access. |
| 49 | + List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly"); |
| 50 | + |
| 51 | + try { |
| 52 | + // Authorize the request. |
| 53 | + Credential credential = Auth.authorize(scopes, "liststreams"); |
| 54 | + |
| 55 | + // This object is used to make YouTube Data API requests. |
| 56 | + youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential) |
| 57 | + .setApplicationName("youtube-cmdline-liststreams-sample") |
| 58 | + .build(); |
| 59 | + |
| 60 | + // Create a request to list liveStream resources. |
| 61 | + YouTube.LiveStreams.List livestreamRequest = youtube.liveStreams().list("id,snippet"); |
| 62 | + |
| 63 | + // Modify results to only return the user's streams. |
| 64 | + livestreamRequest.setMine(true); |
| 65 | + |
| 66 | + // Execute the API request and return the list of streams. |
| 67 | + LiveStreamListResponse returnedListResponse = livestreamRequest.execute(); |
| 68 | + List<LiveStream> returnedList = returnedListResponse.getItems(); |
| 69 | + |
| 70 | + // Print information from the API response. |
| 71 | + System.out.println("\n================== Returned Streams ==================\n"); |
| 72 | + for (LiveStream stream : returnedList) { |
| 73 | + System.out.println(" - Id: " + stream.getId()); |
| 74 | + System.out.println(" - Title: " + stream.getSnippet().getTitle()); |
| 75 | + System.out.println(" - Description: " + stream.getSnippet().getDescription()); |
| 76 | + System.out.println(" - Published At: " + stream.getSnippet().getPublishedAt()); |
| 77 | + System.out.println("\n-------------------------------------------------------------\n"); |
| 78 | + } |
| 79 | + |
| 80 | + } catch (GoogleJsonResponseException e) { |
| 81 | + System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() + " : " |
| 82 | + + e.getDetails().getMessage()); |
| 83 | + e.printStackTrace(); |
| 84 | + |
| 85 | + } catch (IOException e) { |
| 86 | + System.err.println("IOException: " + e.getMessage()); |
| 87 | + e.printStackTrace(); |
| 88 | + } catch (Throwable t) { |
| 89 | + System.err.println("Throwable: " + t.getMessage()); |
| 90 | + t.printStackTrace(); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments