2014-12-24 164 views
4

我試圖通過的OAuth2連接到谷歌。我使用的代碼適用於其他應用程序,所以我很確定問題出在Google的配置中。通過OAuth 2連接到谷歌「無效的參數值REDIRECT_URI:缺少權威:」

我註冊谷歌控制檯客戶端ID和祕密密鑰,我加入到授權的配置:

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文件到另一基站的URL本地主機參數和我下面增加這些位置重定向在谷歌控制檯的URI:

http://localhost:8080/Account/ExternalLoginCallback%3FReturnUrl=/ 
http://localhost.example.com:8080/Account/ExternalLoginCallback 
http://localhost.example.com:8080/Account/ExternalLoginCallback%3FReturnUrl=/ 

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

回答

6

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

相關問題