changexchange
changexchange

Reputation: 63

How can i use Youtube live stream API in c#?

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

Answers (2)

Here is how I managed to make it work:

  1. Create a App - Google Developer Console
  2. Enable the API on your App - Youtube Data API v3
  3. Create a OAuth Client ID - Create OAuth Credential
  4. The application type must be setted to Others
  5. Copy your Client Id and your Client Secret to a safe location.
  6. Access the following URL (you should be logged with the Google Account that will broadcast the live stream):

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

  1. Copy the generated token from the input text box on the page.
  2. 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,
    }
    
  3. 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"
    }
    
  4. Now, that we have all the authentication data needed (client_id, client_secret, refresh_token), it's time to play with the API.
        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

Ibrahim Ulukaya
Ibrahim Ulukaya

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

Related Questions