wpf 我如何在 c# 中使用 Youtube 直播流 API?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29019408/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i use Youtube live stream API in c#?
提问by changexchange
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.
我想制作一个 WPF 应用程序,从我的 ip 摄像头获取视频并将其实时发送到我的 youtube 频道。我环顾了所有网站,但没有示例如何使用 c# 将视频直播到 Youtube。谷歌的网站上有一些例子,但它们是用 PHP、Java 和 Phyton 编写的,但我不知道这种编程语言,所以我无法使用 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.
我试着写一点,但没有奏效。这是我在查看 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();
Please, help me!?!?!?
请帮我!?!?!?
回答by Guilherme Branco Stracini
Here is how I managed to make it work:
这是我设法使它工作的方法:
- Create a App - Google Developer Console
- Enable the API on your App - Youtube Data API v3
- Create a OAuth Client ID - Create OAuth Credential
- The application type must be setted to Others
- Copy your Client Id and your Client Secret to a safe location.
- Access the following URL (you should be logged with the Google Account that will broadcast the live stream):
- 创建应用程序 - Google Developer Console
- 在您的应用程序上启用 API - Youtube Data API v3
- 创建 OAuth 客户端 ID -创建 OAuth 凭据
- 应用类型必须设置为其他
- 将您的客户 ID 和客户密码复制到安全位置。
- 访问以下 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_IDwith your client id generated at step 3
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
使用您在第 3 步中生成的客户端 ID更改CLIENT_ID
- Copy the generated token from the input text box on the page.
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-urlencodedto 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" }- 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; }
- 从页面上的输入文本框中复制生成的令牌。
使用一些工具(cURL、wget、Google Chrome 的 Postman 插件等等)向以下 URL 发出 POST 请求:
https://accounts.google.com/o/oauth2/token
使用以下字段将 HTTP POST x-www-form-urlencoded发送到此 url:(仅更改 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, }如果到这里为止一切看起来都不错,您应该会得到如下响应:
{ "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" }- 现在,我们拥有了所需的所有身份验证数据(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; }
回答by Ibrahim Ulukaya
It seems like you are trying to use ClientLogin as I see username and pass. Instead use OAuth2 and use these samples for guidance.
当我看到用户名并通过时,您似乎正在尝试使用 ClientLogin。而是使用 OAuth2 并使用这些示例作为指导。

