Google 开发人员 API V3,用于在 YouTube C#.net 中上传视频
Google Developer API V3 for video upload in YouTube C#.net
我一直在使用 google 著名的 API V2 从我的网络应用程序将视频上传到 YouTube,因为 API 已经升级到 V3,我无法通过相同的代码上传。
我已经尝试使用新的应用程序将视频上传到 YouTube V3,但是有很多东西是以前有但在 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 来验证用户。无论是网页应用还是控制台应用,代码都是一样的。
/// <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 著名的 API V2 从我的网络应用程序将视频上传到 YouTube,因为 API 已经升级到 V3,我无法通过相同的代码上传。
我已经尝试使用新的应用程序将视频上传到 YouTube V3,但是有很多东西是以前有但在 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 来验证用户。无论是网页应用还是控制台应用,代码都是一样的。
/// <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");