我正在尝试通过 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);
不幸的是,当我按下按钮进行连接时,出现以下错误:
- 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/