11
votes

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:

  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

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

1

1 Answers

14
votes

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:

file:///Account/ExternalLoginCallback%3FReturnUrl=/

you should pass e.g.

http://localhost:8080/Account/ExternalLoginCallback%3FReturnUrl=/

but properly URL-encoded so:

http%3A%2F%2Flocalhost%3A8080%2FAccount%2FExternalLoginCallback%253FReturnUrl%3D%2F

See sample code at: https://github.com/mj1856/DotNetOpenAuth.GoogleOAuth2/blob/master/DotNetOpenAuth.GoogleOAuth2/GoogleOAuth2Client.cs