1+ / *
2+
3+ using System;
4+ using System . IO ;
5+ using System . Reflection ;
6+ using System . Threading ;
7+ using System . Threading . Tasks ;
8+
9+ using Google . Apis . Auth . OAuth2 ;
10+ using Google . Apis . Services ;
11+ using Google . Apis . Upload ;
12+ using Google . Apis . Util . Store ;
13+ using Google . Apis . YouTube . v3 ;
14+ using Google . Apis . YouTube . v3 . Data ;
15+
16+ namespace Google . Apis . YouTube . Samples
17+ {
18+ /// <summary>
19+ /// YouTube Data API v3 sample: retrieve my uploads.
20+ /// Relies on the Google APIs Client Library for .NET, v1.7.0 or higher.
21+ /// See https://code.google.com/p/google-api-dotnet-client/wiki/GettingStarted
22+ /// </summary>
23+ internal class MyUploads
24+ {
25+ [ STAThread ]
26+ static void Main ( string [ ] args )
27+ {
28+ Console . WriteLine ( "YouTube Data API: My Uploads" ) ;
29+ Console . WriteLine ( "============================" ) ;
30+
31+ try
32+ {
33+ new MyUploads ( ) . Run ( ) . Wait ( ) ;
34+ }
35+ catch ( AggregateException ex )
36+ {
37+ foreach ( var e in ex . InnerExceptions )
38+ {
39+ Console . WriteLine ( "Error: " + e . Message ) ;
40+ }
41+ }
42+
43+ Console . WriteLine ( "Press any key to continue..." ) ;
44+ Console . ReadKey ( ) ;
45+ }
46+
47+ private async Task Run ( )
48+ {
49+ UserCredential credential ;
50+ using ( var stream = new FileStream ( "client_secrets.json" , FileMode . Open , FileAccess . Read ) )
51+ {
52+ credential = await GoogleWebAuthorizationBroker . AuthorizeAsync (
53+ GoogleClientSecrets . Load ( stream ) . Secrets ,
54+ // This OAuth 2.0 access scope allows for read-only access to the authenticated
55+ // user's account, but not other types of account access.
56+ new [ ] { YouTubeService . Scope . YoutubeReadonly } ,
57+ "user" ,
58+ CancellationToken . None ,
59+ new FileDataStore ( this . GetType ( ) . ToString ( ) )
60+ ) ;
61+ }
62+
63+ var youtubeService = new YouTubeService ( new BaseClientService . Initializer ( )
64+ {
65+ HttpClientInitializer = credential ,
66+ ApplicationName = this . GetType ( ) . ToString ( )
67+ } ) ;
68+
69+ var channelsListRequest = youtubeService . Channels . List ( "contentDetails" ) ;
70+ channelsListRequest . Mine = true ;
71+
72+ // Retrieve the contentDetails part of the channel resource for the authenticated user's channel.
73+ var channelsListResponse = await channelsListRequest . ExecuteAsync ( ) ;
74+
75+ foreach ( var channel in channelsListResponse . Items )
76+ {
77+ // From the API response, extract the playlist ID that identifies the list
78+ // of videos uploaded to the authenticated user's channel.
79+ var uploadsListId = channel . ContentDetails . RelatedPlaylists . Uploads ;
80+
81+ Console . WriteLine ( "Videos in list {0}" , uploadsListId ) ;
82+
83+ var nextPageToken = "" ;
84+ while ( nextPageToken != null )
85+ {
86+ var playlistItemsListRequest = youtubeService . PlaylistItems . List ( "snippet" ) ;
87+ playlistItemsListRequest . PlaylistId = uploadsListId ;
88+ playlistItemsListRequest . MaxResults = 50 ;
89+ playlistItemsListRequest . PageToken = nextPageToken ;
90+
91+ // Retrieve the list of videos uploaded to the authenticated user's channel.
92+ var playlistItemsListResponse = await playlistItemsListRequest . ExecuteAsync ( ) ;
93+
94+ foreach ( var playlistItem in playlistItemsListResponse . Items )
95+ {
96+ // Print information about each video.
97+ Console . WriteLine ( "{0} ({1})" , playlistItem . Snippet . Title , playlistItem . Snippet . ResourceId . VideoId ) ;
98+ }
99+
100+ nextPageToken = playlistItemsListResponse . NextPageToken ;
101+ }
102+ }
103+ }
104+ }
105+ }
0 commit comments