【问题标题】:Google OAuth shows error web page after successful authentication (using Google API .NET in Desktop application)Google OAuth 在成功验证后显示错误网页(在桌面应用程序中使用 Google API .NET)
【发布时间】:2019-01-13 09:17:42
【问题描述】:

我编写了一些代码来将约会从我的桌面 C# 应用程序导出到 Google 日历。我正在使用适用于 .NET 的 Google Calendar API。

代码运行良好,除了 Web 浏览器在身份验证后总是显示错误页面(尽管如此,身份验证总是成功的)。这是我看到的消息:“无法连接。Firefox 无法在 localhost:xxx(任何端口)中建立与服务器的连接”。它发生在每个浏览器中。

我已使用以下参数在 Google 控制台中创建了我的凭据: - 使用谷歌日历 API; - 从其他 UI(不是 Web 服务器)调用 API - 访问用户数据。

这是我的代码:

public static CalendarService GetCalendarService(String[] Scopes, String userNameGoogle, String ApplicationName)
    {
        UserCredential credential;

        using (var stream =
            new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
        {
            string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            credPath = Path.Combine(credPath, ".credentials/calendar-dotnet-quickstart.json");
            //MessageBox.Show(credPath);

            credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                GoogleClientSecrets.Load(stream).Secrets,
                Scopes,
                userNameGoogle,
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;

            //MessageBox.Show("Credential file saved to: " + credPath);

        }

            // Create Google Calendar API service.
            var service = new CalendarService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName,
            });

            return service;
    } 

验证时会发生以下情况: See Authenticating Image

... 之后,重定向错误: See Redirection error Image

我花了好几个小时阅读帖子,但没有结果(几乎每个人都在使用网络服务器,但不是我的情况)。

拜托,你能帮帮我吗?

【问题讨论】:

  • 请将截图翻译成英文。
  • 您使用的是 Google.Apis.Calendar.v3 nuget 包吗?你用的是哪个版本?
  • @Chris 是的,这就是我使用的版本(Google.Apis.Calendar.v3)

标签: c# google-authentication google-api-dotnet-client


【解决方案1】:

终于,经过这么多天我得到了答案:问题出在杀毒软件(卡巴斯基免费版)上。

我用来测试的三台计算机具有相同的防病毒软件。禁用它后,网络浏览器重定向良好。我很抱歉之前没有测试它。

感谢您的回答(@mjwills、@Chris、@u_1826)。非常感谢。我希望这些信息对其他人有所帮助。

【讨论】:

    【解决方案2】:

    您作为回调 url http://localhost:6773/ 放置的路径存在吗?

    【讨论】:

    • 我说错了什么?看到他的截图,从谷歌返回到本地地址。或者继续回答他
    猜你喜欢
    • 2023-03-09
    • 2022-10-11
    • 2022-10-23
    • 2011-12-26
    • 2019-07-05
    • 2012-11-18
    • 1970-01-01
    • 2012-12-28
    • 1970-01-01
    相关资源
    最近更新 更多