首页 文章

通过OAuth 2连接到Google“redirect_uri的参数值无效:缺少权限:”

提问于
浏览
4

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

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

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

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

那是一个错误 . 错误:invalid_request redirect_uri的参数值无效:缺少权限: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=/

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

1 回答

  • 7

    在按下连接按钮时发送给Google的授权请求中 redirect_uri 参数的值必须设置为您在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

相关问题