我正在运行一个没有 UI 的进程,该进程从 youtube 检索上传的视频。在开发机器上,它代表用户进行身份验证并且可以正常工作。
问题是当进程在服务器上运行时:浏览器窗口不会触发用户让他使用他的帐户(它应该只需要执行一次)。
事件查看器没有显示任何错误。服务帐户似乎不适用于 youtube,API key 的权限太少并且
誓言是验证和获取播放视频的唯一方法。还是我错了?
所以问题是:如何以单个用户的身份运行服务并在没有 UI 的情况下检索他的视频?
private async Task Run()
{
try
{
UserCredential credential;
using (var stream = new FileStream(StartPath + "\\client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { YouTubeService.Scope.YoutubeReadonly, YouTubeService.Scope.Youtube },
"user",
CancellationToken.None,
new FileDataStore("Store")
);
}
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = this.GetType().ToString()
});
....
}
我也试过:
String serviceAccountEmail = "e-mail";
var certificate = new X509Certificate2(HostingEnvironment.MapPath("~/Content/key.p12"), "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { YouTubeService.Scope.Youtube, YouTubeService.Scope.YoutubepartnerChannelAudit, YouTubeService.Scope.YoutubeUpload }
}.FromCertificate(certificate));
var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "api",
});
最佳答案
YouTube 数据 API 可让您将通常在 YouTube 网站上执行的功能整合到您自己的网站或应用程序中。下面的列表确定了您可以使用 API 检索的不同类型的资源。 API 还支持插入、更新或删除其中许多资源的方法。
本引用指南解释了如何使用 API 来执行所有这些操作。该指南按资源类型组织。资源代表构成 YouTube 体验的一部分的项目类型,例如视频、播放列表或订阅。对于每种资源类型,该指南列出了一个或多个数据表示,并且资源表示为 JSON 对象。该指南还列出了一种或多种支持的方法(LIST, POST, DELETE, etc.)
对于每种资源类型,并解释如何在您的应用程序中使用这些方法。
以下要求适用于 YouTube 数据 API 请求:
您的 API key 在 Developer Console's 中可用 API 访问 您的项目的 Pane 。
此外,一些用于检索资源的 API 方法可能支持需要授权的参数,或者在请求被授权时可能包含额外的元数据。例如,如果请求由特定用户授权,则检索用户上传视频的请求也可能包含私有(private)视频。
access_token
像这样的查询参数:? access_token=oauth2-token
Authorization
像这样的标题:Authorization: Bearer oauth2-token
在您的应用程序中实现 OAuth 2.0 身份验证的完整说明可在 authentication guide 中找到。 . 关于c# - 在工作进程 C# 上验证 Youtube API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39284672/