asp.net-mvc-4 - 通过 OAuth 2 "Invalid parameter value for redirect_uri: Missing authority: "连接到 Google

标签 asp.net-mvc-4 mono oauth-2.0 google-oauth

我正在尝试通过 OAuth2 连接到 Google。我正在使用在另一个应用程序中工作的代码,所以我很确定问题出在 Google 的配置中。

我在 Google 控制台中注册了一个客户端 ID 和 key ,并将其添加到授权配置中:

var client = new GoogleOAuth2Client("[client id].apps.googleusercontent.com", "[secret key]");
var extraData = new Dictionary<string, object>();
OAuthWebSecurity.RegisterClient(client, "Google", extraData);

不幸的是,当我按下按钮进行连接时,出现以下错误:

  1. That’s an error.

    Error: invalid_request

    Invalid parameter value for redirect_uri: Missing authority:
    file:///Account/ExternalLoginCallback%3FReturnUrl=/

    Request Details
    scope=https://www.googleapis.com/auth/userinfo.profile
    https://www.googleapis.com/auth/userinfo.email
    response_type=code
    redirect_uri=file:///Account/ExternalLoginCallback%3FReturnUrl=/
    state=provider=google&sid=[numbers] client_id=[client id].apps.googleuserconte


我尝试更改 /etc/hosts 中的 localhost 参数文件到其他基本 URL,我已添加这些位置以重定向 Google 控制台中的 URI,如下所示:
http://localhost:8080/Account/ExternalLoginCallback%3FReturnUrl=/ 
http://localhost.example.com:8080/Account/ExternalLoginCallback
http://localhost.example.com:8080/Account/ExternalLoginCallback%3FReturnUrl=/

错误仍然存​​在。我不知道可能是什么问题,我希望有人能给我一些指导。谢谢

最佳答案

redirect_uri的值按下连接按钮后发送给 Google 的授权请求中的参数必须设置为您在 Google API 控制台中为您的客户端注册的值之一。所以,而不是通过:

file:///Account/ExternalLoginCallback%3FReturnUrl=/

你应该通过例如
http://localhost:8080/Account/ExternalLoginCallback%3FReturnUrl=/

但正确的 URL 编码所以:
http%3A%2F%2Flocalhost%3A8080%2FAccount%2FExternalLoginCallback%253FReturnUrl%3D%2F

参见示例代码:https://github.com/mj1856/DotNetOpenAuth.GoogleOAuth2/blob/master/DotNetOpenAuth.GoogleOAuth2/GoogleOAuth2Client.cs

关于asp.net-mvc-4 - 通过 OAuth 2 "Invalid parameter value for redirect_uri: Missing authority: "连接到 Google,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27640209/

相关文章:

c# - Math.Round 没有正确四舍五入到小数点后 2 位

c# - 在 Unity3d 中处理文件时使用 System.IO 是否安全?

oauth-2.0 - 使用 phpleague/oauth2-client 发出 POST 请求

android - OAuth 示例应用程序因验证错误而崩溃

c# - MVC4 动态 Controller 路径?

asp.net-mvc-4 - MVC4 与 TinyMCE bundle

javascript - 是否可以获取 View 上每个 div 的坐标并将它们保存在 javascript onunload() 函数中的本地存储中?

c# - 检查对象是字典还是列表

c# - Mac OS X Mono ASP.NET和C#编译过程

java - Spring Boot Oauth2 注销端点