如何在c#中使用Youtube直播API?

时间:2015-03-12 20:16:38

标签: c# wpf youtube-livestreaming-api

我想创建一个WPF应用程序,从我的ip camera获取视频并将其发送到我的youtube频道。我浏览了所有的网站,但没有例子我怎么能用c#将视频直播到Youtube。谷歌网站上有一些例子,但它们是用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();

请帮帮我!?!?!?

2 个答案:

答案 0 :(得分:1)

看起来您正在尝试使用ClientLogin,因为我看到用户名和通行证。 而是使用OAuth2并使用these samples for guidance

答案 1 :(得分:1)

以下是我设法使其发挥作用的方式:

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

    https://accounts.google.com/o/oauth2/auth?client_id= CLIENT_ID &安培;范围= https://gdata.youtube.com&response_type=code&access_type=offline&redirect_uri=urn:ietf:wg:oauth:2.0:oob

         

    使用第3步

    生成的客户ID更改 CLIENT_ID
    1. 从页面上的输入文本框中复制生成的令牌。
    2. 使用某种工具(cURL,wget,Google Chrome的Postman插件,无论......)向以下网址发出POST请求:

        

      https://accounts.google.com/o/oauth2/token

           

      使用以下字段对此网址进行HTTP POST x-www-form-urlencoded :(仅更改client_id,client_token和代码,首先保留2)。

      {
          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;
          }