gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:33:09 26 4
gpt4 key购买 nike

我想制作一个 WPF 应用程序,从我的网络摄像机获取视频并将其实时发送到我的 youtube channel 。我环顾了所有网站,但没有示例如何使用 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();

请帮帮我!?!?!?

最佳答案

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

  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. 从页面上的输入文本框中复制生成的 token 。
  2. 使用一些工具(cURL、wget、Google Chrome 的 Postman 插件等)向以下 URL 发出 POST 请求:

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

关于c# - 我如何在 C# 中使用 Youtube 直播流 API?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019408/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com