GoogleWebAuthorizationBroker.AuthorizeAsync冻结

Har*_*Boy 8 c# google-api youtube-api oauth-2.0 google-api-dotnet-client

我有一个C#.net网络应用程序,用于将视频上传到Youtube.这工作大约6个月前,但现在已停止工作.当我在本地计算机上通过Visual Studio启动我的网站时,以下代码会挂起:

     UserCredential credential;
    using (var auth_stream = new FileStream(Server.MapPath("~/YouTube/client_secrets.json"), FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.Load(auth_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
        );
    }
Run Code Online (Sandbox Code Playgroud)

在查看堆栈溢出的其他帖子之后我也尝试了以下内容:

     UserCredential credentialS;
     using (var auth_stream = new FileStream(Server.MapPath("~/YouTube/client_secrets.json"), FileMode.Open, FileAccess.Read))
     {
         credentialS = await GoogleWebAuthorizationBroker.AuthorizeAsync(
             new ClientSecrets
             {
                 ClientId = "blahblah-myDetailsBlahBLah.apps.googleusercontent.com",
                 ClientSecret = "myClientSecret",
             },
             // 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
         );
     }
Run Code Online (Sandbox Code Playgroud)

我还注意到我使用的是Google API的1.7.0-beta版本,因此我通过PackageManager控制台升级到版本1.8.2.

我还通过Google Developer Console生成了一个新的客户端密钥,并在我的网站中使用了它.

但在尝试这些步骤后,对GoogleWebAuthorizationBroker.AuthorizeAsync的调用仍然挂起.我注意到我的本地浏览器中的网站刷新,好像它试图连接但是呼叫永远不会成功.

我怎么解决这个?

小智 1

我在使用 Google Calendar API 时也遇到了这个问题。

我的应用程序正在从各种在线资源中抓取通知并将其发布到共享的谷歌日历中。

它在开发环境中运行良好,但当我将其部署到虚拟机时,它使用该虚拟机中的服务帐户每 15 分钟作为 Windows 计划任务运行一次,但效果不佳。

我记得第一次设置它时,我必须在浏览器中执行授权步骤,但作为计划任务运行时不会触发它。

我找不到一种方法让它通过浏览器提示再次授予访问权限。

由于服务帐户 C:\Users\\Documents.credentials\calendar-dotnet-quickstart 文件夹创建得很好,我只需从我的开发中的同一文件夹复制 Google.Apis.Auth.OAuth2.Responses.TokenResponse-user环境,现在工作正常。

这可能不是最好或最终的解决方案,但它确实有效。

我怀疑如果我作为计划任务的服务用户登录并从命令提示符运行,我会通过浏览器收到授予授权的提示。

希望这对某人有帮助。