我如何在 C# 中使用 Youtube 直播流 API?

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

我想制作一个 WPF 应用程序,从我的网络摄像机获取视频并将其实时发送到我的 YouTube 频道。我环顾了所有网站,但没有示例如何使用 c# 将视频直播到 Youtube。 google 的网站上有示例,但它们是用 PHP、Java 和 Phyton 编写的,但我不知道这些编程语言,所以我无法使用 API.

我试着写了一点,但没有成功。这是我通过 Java 示例编写的代码。

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();

请帮帮我!?!?!?

根据我看到的用户名和密码,您似乎正在尝试使用 ClientLogin。 而是使用 OAuth2 并使用 these samples for guidance.

以下是我设法让它发挥作用的方法:

  1. 创建应用程序 - Google Developer Console
  2. 在您的应用程序上启用 API - Youtube Data API v3
  3. 创建 OAuth 客户端 ID - Create OAuth Credential
  4. 申请类型必须设置为其他
  5. 将您的客户端 ID 和客户端密码复制到安全位置。
  6. 访问以下 URL(您应该使用将直播直播的 Google 帐户登录):

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. 从页面上的输入文本框中复制生成的令牌。
  2. 使用一些工具(cURL、wget、Google Chrome 的 Postman 插件,等等...)发出一个 POST 请求到以下 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. 如果到这里为止一切正常,您应该会得到这样的回复:

    {
    "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. 现在,我们已拥有所需的所有身份验证数据(client_id、client_secret、refresh_token),是时候使用 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;
        }