gpt4 book ai didi

c# - 在YouTube C#.net中用于视频上传的Google Developer API V3

转载 作者:行者123 更新时间:2023-12-03 05:32:18 26 4
gpt4 key购买 nike

我一直在使用Google知名的API V2从我的Web应用程序在YouTube上上传视频,因为API逐渐升级到了V3,我无法通过相同的代码上传相同的视频。

我已经尝试过使用V3在YouTube上上传视频的新应用程序,但是之前有很多东西在V3中不可用

旧:
来自:Google.Gdata.YouTube.Youtubeservice

YouTubeService service = new YouTubeService(applicationName,developerKey);
service.setUserCredentials(googleEmail, googleEmailPassword);

新:发件人:Google.Apis.Youtube.V3.YoutubeService
YouTubeService service = new YouTubeService();` -- its doesn't have the set credentials

我已经看过 YouTube Data API .NET Code Samples,但是它不是一个控制台应用程序,因此并没有太大用处。

最佳答案

您不能再使用登录名和密码。您需要使用Oauth2对用户进行身份验证。如果是Web或控制台应用程序,则代码是相同的。

/// <summary>
/// Authenticate to Google Using Oauth2
/// Documentation https://developers.google.com/accounts/docs/OAuth2
/// </summary>
/// <param name="clientId">From Google Developer console https://console.developers.google.com</param>
/// <param name="clientSecret">From Google Developer console https://console.developers.google.com</param>
/// <param name="userName">A string used to identify a user.</param>
/// <returns></returns>
public static YouTubeService AuthenticateOauth(string clientId, string clientSecret, string userName)
{

string[] scopes = new string[] { YouTubeService.Scope.Youtube, // view and manage your YouTube account
YouTubeService.Scope.YoutubeForceSsl,
YouTubeService.Scope.Youtubepartner,
YouTubeService.Scope.YoutubepartnerChannelAudit,
YouTubeService.Scope.YoutubeReadonly,
YouTubeService.Scope.YoutubeUpload};

try
{
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
, scopes
, userName
, CancellationToken.None
, new FileDataStore("Daimto.YouTube.Auth.Store")).Result;

YouTubeService service = new YouTubeService(new YouTubeService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "YouTube Data API Sample",
});
return service;
}
catch (Exception ex)
{
Console.WriteLine(ex.InnerException);
return null;

}

} }

用法:
 // Authenticate Oauth2
String CLIENT_ID = "xxxxx-d0vpdthl4ms0soutcrpe036ckqn7rfpn.apps.googleusercontent.com";
String CLIENT_SECRET = "NDmluNfTgUk6wgmy7cFo64RV";
var service = Authentication.AuthenticateOauth(CLIENT_ID, CLIENT_SECRET, "test");

Google-dotnet-Samples/ youtube翻录的代码

关于c# - 在YouTube C#.net中用于视频上传的Google Developer API V3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30755397/

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