Asp.net-Mvc-4

通過 OAuth 2 連接到 Google“redirect_uri 的參數值無效:缺少權限:”

  • December 30, 2014

我正在嘗試通過 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);

不幸的是,當我按下按鈕進行連接時,出現以下錯誤:

  1. 那是一個錯誤。

錯誤:invalid_request

redirect_uri 的參數值無效:缺少權限:
file:///Account/ExternalLoginCallback%3FReturnUrl=/

請求詳細資訊
範圍= 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=/

錯誤仍然存在。我不知道問題可能是什麼,我希望有人能給我一些指導。謝謝

在按下按鈕進行連接時發送給 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

引用自:https://stackoverflow.com/questions/27640209