gpt4 book ai didi

.net - googleWebAuthorizationBroker.AuthorizeAsync 永远挂起

转载 作者:行者123 更新时间:2023-12-01 05:09:59 26 4
gpt4 key购买 nike

我尝试使用 youtube api v3 和 .NET 将视频上传到 youtube,但它会永远挂起调用 AuthorizeAsync。您还可以在以下位置查看代码:https://developers.google.com/youtube/v3/code_samples/dotnet

  static void Main(string[] args)
{
Console.WriteLine("YouTube Data API: Upload Video");
Console.WriteLine("==============================");

try
{
new UploadVideo().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("Error: " + e.Message);
}
}

Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}

private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
// This OAuth 2.0 access scope allows an application to upload files to the
// authenticated user's YouTube channel, but doesn't allow other types of access.
new[] { YouTubeService.Scope.YoutubeUpload },
"user",
CancellationToken.None
);
}

var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = Assembly.GetExecutingAssembly().GetName().Name
});

最佳答案

我找到了解决方案。代码不起作用,因为应用程序托管在 IIS 上。 IIS 访问 %appdata% 文件夹内的文件夹可能存在问题。 token 文件默认保存在 %appdata% 文件夹中。我尝试更改对该文件夹的权限。我添加了 Everyone,DefaultAppPool,I_USER,... 但没有成功。还有更新版本的 Google.Apis (1.8.2) 允许完整路径( new FileDataStore(fullPath,true)) true 表示完整路径,但即使存在错误。无法使用该构造函数,因为我总是出错。所以我决定我的应用程序应该使用 IIS Express(实际上我创建了带有授权代码的新项目)。转到解决方案属性并将您的应用设置为使用 IIS Express。

https://console.developers.google.com 获取 cliendId(json 文件) (选择 Web 应用程序)并将 http://localhost:48372 (或任何您的应用程序端口使用)设置为 javascript 来源,并将 http://localhost:48372/authorize.aspx 设置为重定向 url。您还应该在互联网上找到如何使您的端口静态)。保存您的 clientId 文件。 (这一步你可能已经做过了)
当您使用 IIS express 时, AuthorizeAsync 方法应该没有问题。系统将提示您输入凭据并选择您的 youtube 帐户(如果您已经登录,则只需选择帐户)。当您授予您的应用权限时,您将被重定向到 authorize.aspx。您的授权 aspx 页面应创建新的 IAuthorization 流对象,并提供 Client Secret 属性和 DataStore 属性。使用 code= Request.Params["code"] 从 URL 读取代码并创建 UserCredential 对象。 youtubeService 的 HttpClientInitializer 属性是 UserCredential 类型。您应该使用的方法类似于 var usercredential= flow.SomeMethodWhichReceives("user",code,CancelletionToken.None)。之后,在您指定的 FileDataStore 文件夹中生成包含刷新 token 的文件。
当您拥有刷新 token 时,您可以做任何您想做的事情,因为刷新 token 应该永远持续(除非手动撤销)。有一种方法允许从 refreshtoken 获取用户凭据,例如 flow.getAccessTokenfromRefreshToken("user","your_refresh_token_string")
因此,当您拥有刷新 token 并使用最后一种方法时,您将能够访问 google youtube api。您还可以使用这种方法将其他用户上传到您的 youtube 帐户。如果您对用户放入您帐户的内容感到满意,您可以随时将他们上传的视频设为私有(private),并在您验证它们时将其更改为公开。

google youtube api 中的某些方法需要 OAUTH2 身份验证(从客户端密码获取刷新 token -> 从刷新 token 获取访问 token ...),例如上传视频,但有些方法(例如 getvideos)不需要。您可以向您的 youtubeService 构造器提供 API key ,而不是 UserCredential

编辑:还有一个我忘了提到的问题(对于那些认为这就是全部的人)。当您授予应用访问 google api 的权限时(就在您被重定向到 authorize.aspx 之前),您将收到错误 redirect_url mismatch,因为 url 参数 redirect_url 中的某个位置与您在 ClientId 中提供的重定向 url 不匹配json,(端口不同,至少,出于某种原因)。更改此参数,使其与您在 clientId json 中提供的重定向 url 匹配。

关于.net - googleWebAuthorizationBroker.AuthorizeAsync 永远挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25629183/

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