【问题标题】:Redirect_URI_missmatch: error while authenticating to the on Google Plus API with c#?Redirect_URI_missmatch:使用 C# 对 Google Plus API 进行身份验证时出错?
【发布时间】:2017-06-29 13:54:39
【问题描述】:

我按照这里的示例Google+ API List with C# 我使用此代码对 Google plus API 进行身份验证:

string[] scopes = new string[] {PlusService.Scope.PlusLogin,
            PlusService.Scope.UserinfoEmail,
            PlusService.Scope.UserinfoProfile
            };
        // here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
        UserCredential credential =
            GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
                {                

                    ClientId = "xxxx-jh7244ds8ruseviasg0a5g6ln09p0l44.apps.googleusercontent.com",
                    ClientSecret = "e5kjgbVJz8o92Xqb7RvpzqPk"
            },
                scopes,
                Environment.UserName,
                CancellationToken.None,
                new FileDataStore("Daimto.GooglePlus.Auth.Store")
            ).Result;

当我运行代码时,我得到以下错误:

即使是在 http://localhost:53308/ 上运行的程序也是我在 Google 开发者控制台中的设置。

【问题讨论】:

  • 尝试在您的项目上启用 SSL (Web Project->Properties->SSL Enabled = true),然后将这些 URL 用于您的 google 凭据。
  • 出现同样的错误
  • 您收到的错误似乎表明您正在重定向到未经控制台授权的 URL。

标签: c# asp.net-mvc-4 google-api google-plus google-api-dotnet-client


【解决方案1】:

当您在 Visual stuido 中运行代码时,除非您告诉它不要告诉它,否则 Visual Studio 会在您每次运行时添加随机端口号。

您发送的重定向 URI 必须与您在 Google 开发者控制台中输入的完全匹配

你目前正在从

http://localhost:53732/authorize

您在开发者控制台中设置的唯一重定向 uri 是

http://lostlhost:53308/sign-in

修复 Visual Studio,使其停止添加随机端口,然后将该端口添加到开发者控制台页面

问题二:

您使用的代码用于已安装的应用程序而不是 Web 应用程序。它可以在您的本地计算机上正常工作,因为您可以访问打开 Web 浏览器。但是,当您将其部署到您的网络服务器时,它不会起作用。你将不得不使用类似 Web applications (ASP.NET MVC)

的东西
private static readonly IAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
                {
                    ClientSecrets = new ClientSecrets
                    {
                        ClientId = "PUT_CLIENT_ID_HERE",
                        ClientSecret = "PUT_CLIENT_SECRET_HERE"
                    },
                    Scopes = new[] { DriveService.Scope.Drive },
                    DataStore = new FileDataStore("Drive.Api.Auth.Store")
                });

【讨论】:

  • 记住你是在重定向 uri 而不是 JS 源之后。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-23
  • 2013-06-24
  • 2019-06-03
  • 1970-01-01
  • 1970-01-01
  • 2017-03-11
  • 1970-01-01
相关资源
最近更新 更多