Reputation: 63
I want to make a WPF application that gets the video from my ip camera and sends it to my youtube channel live. I look around all of the websites but there's no example how can i live stream a video to Youtube with c#. There are examples in google's website but they were written with PHP, Java and Phyton but I don't know this programming languages so i couldn't use the API.
I tried to write a little bit but it didn't work. Here's my code that i wrote looking through the Java example.
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets { ClientId = "MyClientId", ClientSecret = "MyClientSecret" },
new[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile },
"My Youtube Channel Name",
CancellationToken.None,
new FileDataStore("Drive.Auth.Store")).Result;
string devkey = "AIzaSyCbxm6g9orAw9PF3MkzTb_0PGbpD3Xo1Qg";
string username = "MyYoutubeChannelEmailAdress";
string password = "MyPassword";
YouTubeRequestSettings youtubereqsetting = new YouTubeRequestSettings("API Project", devkey, username, password);
YouTubeRequest youtubereq = new YouTubeRequest(youtubereqsetting);
LiveBroadcastSnippet broadcastSnippet = new LiveBroadcastSnippet();
broadcastSnippet.Title = "Test Live Stream";
broadcastSnippet.ScheduledStartTime = new DateTime(2015, 3, 12, 19, 00, 00);
broadcastSnippet.ScheduledEndTime = new DateTime(2015, 3, 12, 20, 00, 00);
LiveBroadcastStatus status = new LiveBroadcastStatus();
status.PrivacyStatus = "Private";
LiveBroadcast broadcast = new LiveBroadcast();
broadcast.Kind = "youtube#liveBroadcast";
broadcast.Snippet = broadcastSnippet;
broadcast.Status = status;
Google.Apis.YouTube.v3.LiveBroadcastsResource.InsertRequest liveBroadcastInsert = new Google.Apis.YouTube.v3.LiveBroadcastsResource.InsertRequest(service, broadcast, "");
LiveBroadcast returnLiveBroadcast = liveBroadcastInsert.Execute();
Please, help me!?!?!?
Upvotes: 4
Views: 9710
Reputation: 299
Here is how I managed to make it work:
https://accounts.google.com/o/oauth2/auth?client_id=CLIENT_ID&scope=https://gdata.youtube.com&response_type=code&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob
Change the CLIENT_ID with your client id generated at step 3
Using some tool (cURL, wget, Postman plugin for Google Chrome, whatever...) make a POST request to the following URL:
https://accounts.google.com/o/oauth2/token
Make a HTTP POST x-www-form-urlencoded to this url with the following fields: (Change only client_id, client_token and code, the 2 first leave as it).
{ grant_type=authorization_code, redirect_uri=urn:ietf:wg:oauth2.0:oob, code=token_from_step_6_&_7 client_id=your_client_id, client_secret=your_client_secret, }
If all looks good until here, you should get a response like this one:
{ "access_token" : "token valid for next few minutes. WE DON'T WANT THIS ONE", "token_type" : "Bearer", "expires_in" : 3600, "refresh_token" : "token valid for offline app. COPY THIS ONE, AND STORE IT" }
public String CreateLiveBroadcastEvent(String eventTitle, DateTime eventStartDate) { ClientSecrets secrets = new ClientSecrets() { ClientId = CLIENT_ID, ClientSecret = CLIENT_SECRET }; var token = new TokenResponse { RefreshToken = REFRESH_TOKEN }; var credentials = new UserCredential(new GoogleAuthorizationCodeFlow( new GoogleAuthorizationCodeFlow.Initializer { ClientSecrets = secrets }), "user", token); var service = new YouTubeService(new BaseClientService.Initializer { HttpClientInitializer = credentials, ApplicationName = "your-app-name" }); var broadcast = new LiveBroadcast { Kind = "youtube#liveBroadcast", Snippet = new LiveBroadcastSnippet { Title = eventTitle, ScheduledStartTime = eventStartDate }, Status = new LiveBroadcastStatus { PrivacyStatus = "public" } }; var request = service.LiveBroadcasts.Insert(broadcast, "id,snippet,status"); var response = request.Execute(); return response.Id; }
Upvotes: 4
Reputation: 12877
It seems like you are trying to use ClientLogin as I see username and pass. Instead use OAuth2 and use these samples for guidance.
Upvotes: 1