Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connecting to Google via OAuth 2 "Invalid parameter value for redirect_uri: Missing authority: "

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

like image 849
Erwin Rooijakkers Avatar asked Dec 24 '14 17:12

Erwin Rooijakkers


People also ask

What is redirect URI in oauth2 Google?

The redirect URIs are the endpoints to which the OAuth 2.0 server can send responses. These endpoints must adhere to Google's validation rules. For testing, you can specify URIs that refer to the local machine, such as http://localhost:8080 .

How do I add authorized redirect to URIs?

To modify your app's allowed redirect URIs, go to console.cloud.google.com , click the left side panel, and navigate to APIs & Services > Credentials . From there, find the OAuth credential that you want to modify. Click "Edit" and you should see a list of "Authorized redirect URIs".


1 Answers

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

like image 138
Hans Z. Avatar answered Oct 01 '22 09:10

Hans Z.