I am trying to connect to Google via OAuth2. I am using code which works in another application, so I am quite sure the problem is in the configuration at Google.
I registered a client-id and secret-key in the Google Console which I added to the authorization config:
var client = new GoogleOAuth2Client("[client id].apps.googleusercontent.com", "[secret key]");
var extraData = new Dictionary<string, object>();
OAuthWebSecurity.RegisterClient(client, "Google", extraData);
Unfortunately, when I press the button to connect I get the following error:
- 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
I've tried changing the localhost parameter in /etc/hosts
file to other base URLs and I've added these locations to redirect URIs in Google Console as follows:
http://localhost:8080/Account/ExternalLoginCallback%3FReturnUrl=/
http://localhost.example.com:8080/Account/ExternalLoginCallback
http://localhost.example.com:8080/Account/ExternalLoginCallback%3FReturnUrl=/
The error persists. I don't know what the problem can be and I hope someone can give me some guidance. Thanks
Best Answer
The value of the
redirect_uri
parameter in the authorization request that is sent to Google upon pressing the button to connect must be set to one of the values that you have registered for you client in the Google API Console. So instead of passing:you should pass e.g.
but properly URL-encoded so:
See sample code at: https://github.com/mj1856/DotNetOpenAuth.GoogleOAuth2/blob/master/DotNetOpenAuth.GoogleOAuth2/GoogleOAuth2Client.cs