|
| 1 | +using System; |
| 2 | +using System.Collections; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Reflection; |
| 5 | + |
| 6 | +/* |
| 7 | + * External dependencies, OAuth 2.0 support, and core client libraries are at: |
| 8 | + * https://code.google.com/p/google-api-dotnet-client/wiki/APIs#YouTube_Data_API |
| 9 | + * Also see the Samples.zip file for the Google.Apis.Samples.Helper classes at: |
| 10 | + * https://code.google.com/p/google-api-dotnet-client/wiki/Downloads |
| 11 | + */ |
| 12 | + |
| 13 | +using DotNetOpenAuth.OAuth2; |
| 14 | + |
| 15 | +using Google.Apis.Authentication; |
| 16 | +using Google.Apis.Authentication.OAuth2; |
| 17 | +using Google.Apis.Authentication.OAuth2.DotNetOpenAuth; |
| 18 | +using Google.Apis.Samples.Helper; |
| 19 | +using Google.Apis.Services; |
| 20 | +using Google.Apis.Util; |
| 21 | +using Google.Apis.Youtube.v3; |
| 22 | +using Google.Apis.Youtube.v3.Data; |
| 23 | + |
| 24 | +namespace dotnet |
| 25 | +{ |
| 26 | + class my_uploads |
| 27 | + { |
| 28 | + static void Main(string[] args) |
| 29 | + { |
| 30 | + CommandLine.EnableExceptionHandling(); |
| 31 | + CommandLine.DisplayGoogleSampleHeader("YouTube Data API: My Uploads"); |
| 32 | + |
| 33 | + var credentials = PromptingClientCredentials.EnsureFullClientCredentials(); |
| 34 | + var provider = new NativeApplicationClient(GoogleAuthenticationServer.Description) |
| 35 | + { |
| 36 | + ClientIdentifier = credentials.ClientId, |
| 37 | + ClientSecret = credentials.ClientSecret |
| 38 | + }; |
| 39 | + var auth = new OAuth2Authenticator<NativeApplicationClient>(provider, GetAuthorization); |
| 40 | + |
| 41 | + var youtube = new YoutubeService(new BaseClientService.Initializer() |
| 42 | + { |
| 43 | + Authenticator = auth |
| 44 | + }); |
| 45 | + |
| 46 | + var channelsListRequest = youtube.Channels.List("contentDetails"); |
| 47 | + channelsListRequest.Mine = true; |
| 48 | + |
| 49 | + var channelsListResponse = channelsListRequest.Fetch(); |
| 50 | + |
| 51 | + foreach (var channel in channelsListResponse.Items) |
| 52 | + { |
| 53 | + var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads; |
| 54 | + |
| 55 | + CommandLine.WriteLine(String.Format("Videos in list {0}", uploadsListId)); |
| 56 | + |
| 57 | + var nextPageToken = ""; |
| 58 | + while (nextPageToken != null) |
| 59 | + { |
| 60 | + var playlistItemsListRequest = youtube.PlaylistItems.List("snippet"); |
| 61 | + playlistItemsListRequest.PlaylistId = uploadsListId; |
| 62 | + playlistItemsListRequest.MaxResults = 50; |
| 63 | + playlistItemsListRequest.PageToken = nextPageToken; |
| 64 | + |
| 65 | + var playlistItemsListResponse = playlistItemsListRequest.Fetch(); |
| 66 | + |
| 67 | + foreach (var playlistItem in playlistItemsListResponse.Items) |
| 68 | + { |
| 69 | + CommandLine.WriteLine(String.Format("{0} ({1})", playlistItem.Snippet.Title, playlistItem.Snippet.ResourceId.VideoId)); |
| 70 | + } |
| 71 | + |
| 72 | + nextPageToken = playlistItemsListResponse.NextPageToken; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + CommandLine.PressAnyKeyToExit(); |
| 77 | + } |
| 78 | + |
| 79 | + private static IAuthorizationState GetAuthorization(NativeApplicationClient client) |
| 80 | + { |
| 81 | + var storage = MethodBase.GetCurrentMethod().DeclaringType.ToString(); |
| 82 | + var key = "storage_key"; |
| 83 | + |
| 84 | + IAuthorizationState state = AuthorizationMgr.GetCachedRefreshToken(storage, key); |
| 85 | + if (state != null) |
| 86 | + { |
| 87 | + client.RefreshToken(state); |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + state = AuthorizationMgr.RequestNativeAuthorization(client, YoutubeService.Scopes.YoutubeReadonly.GetStringValue()); |
| 92 | + AuthorizationMgr.SetCachedRefreshToken(storage, key, state); |
| 93 | + } |
| 94 | + |
| 95 | + return state; |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments