我想创建一个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();
请帮帮我!?!?!?
答案 0 :(得分:1)
看起来您正在尝试使用ClientLogin,因为我看到用户名和通行证。 而是使用OAuth2并使用these samples for guidance。
答案 1 :(得分:1)
以下是我设法使其发挥作用的方式:
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
使用某种工具(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, }
如果在此之前看起来都很好,你应该得到像这样的答案:
{ "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" }
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; }