c# - GoogleWebAuthorizationBroker.AuthorizeAsync 卡住

标签 c# oauth-2.0 google-api youtube-api google-api-dotnet-client

我有一个 C# .net Web 应用程序,用于将视频上传到 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
        );
    }

在查看有关堆栈溢出的其他帖子后,我还尝试了以下操作:

     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
         );
     }

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

我还通过 Google Developer Console 生成了一个新的 Client Secret,并在我的网站中使用了它。

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

我该如何解决这个问题??

最佳答案

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

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

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

我记得第一次设置它时,我必须在浏览器中完成授权步骤,但当它作为计划任务运行时并没有被触发。

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

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

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

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

希望这对某人有帮助。

关于c# - GoogleWebAuthorizationBroker.AuthorizeAsync 卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32169124/

相关文章:

android - Google Api 刷新 token 说明

oauth - Google API 刷新 token 已过期或被撤销

Java - 下载并删除 Google Drive 上的文件

c# - 使用 Automapper (.net c#) 映射到不在 Src 中的变量以便在 linq2sql 类中使用?

c# - 使用 rand.Next() 重复结果的概率

python - 用于访问用户 OneDrive 的 Microsoft Graph API 代码库解决方案

spring-security - Spring oauth2 - 授权代码在使用后删除

javascript - 如何删除 google api 图表折线图中的注释刻度线?

c# - Function C++ 到 C#(安全代码)

c# - 列不是 PK 时的 EF6 MySQL StrongTypingException