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: create a playlist.
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 PlaylistUpdates
24+ {
25+ [ STAThread ]
26+ static void Main ( string [ ] args )
27+ {
28+ Console . WriteLine ( "YouTube Data API: Playlist Updates" ) ;
29+ Console . WriteLine ( "==================================" ) ;
30+
31+ try
32+ {
33+ new PlaylistUpdates ( ) . 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 full read/write access to the
55+ // authenticated user's account.
56+ new [ ] { YouTubeService . Scope . Youtube } ,
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+ // Create a new, private playlist in the authorized user's channel.
70+ var newPlaylist = new Playlist ( ) ;
71+ newPlaylist . Snippet = new PlaylistSnippet ( ) ;
72+ newPlaylist . Snippet . Title = "Test Playlist" ;
73+ newPlaylist . Snippet . Description = "A playlist created with the YouTube API v3" ;
74+ newPlaylist . Status = new PlaylistStatus ( ) ;
75+ newPlaylist . Status . PrivacyStatus = "public" ;
76+ newPlaylist = await youtubeService . Playlists . Insert ( newPlaylist , "snippet,status" ) . ExecuteAsync ( ) ;
77+
78+ // Add a video to the newly created playlist.
79+ var newPlaylistItem = new PlaylistItem ( ) ;
80+ newPlaylistItem . Snippet = new PlaylistItemSnippet ( ) ;
81+ newPlaylistItem . Snippet . PlaylistId = newPlaylist . Id ;
82+ newPlaylistItem . Snippet . ResourceId = new ResourceId ( ) ;
83+ newPlaylistItem . Snippet . ResourceId . Kind = "youtube#video" ;
84+ newPlaylistItem . Snippet . ResourceId . VideoId = "GNRMeaz6QRI" ;
85+ newPlaylistItem = await youtubeService . PlaylistItems . Insert ( newPlaylistItem , "snippet" ) . ExecuteAsync ( ) ;
86+
87+ Console . WriteLine ( "Playlist item id {0} was added to playlist id {1}." , newPlaylistItem . Id , newPlaylist . Id ) ;
88+ }
89+ }
90+ }
0 commit comments