Error 400: redirect_uri_mismatch trying to access Google Drive

Viewed 989

I know there are a ton of questions like this already, but none of the answers have helped me. I've listed all the suggested points at the end, please read there to see if I've missed anything.

I'm using the Google Drive C# API, and have a web site (Blazor, .NET5 in case it makes any difference) that has been working fine. I was using the client's Google credentials for this, had downloaded a JSON file (following instructions here) and all was well. When I first ran the web site in Visual Studio, I got the Google auth screen, in which I entered his email and password, and it worked. This created a JSON file in my web site which then allowed me to run the site next time without having to auth again.

In the credentials JSON file, I added the redirect URI, using the URI that VS uses when I debug the site. This gave me JSON file like this...

{
  "web": {
    "client_id": "xxxxxxxxxxxxxxxx",
    "project_id": "xxxxxxxxxxxxxxx",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://oauth2.googleapis.com/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_secret": "xxxxxxxxxxxxxxxxxxxxx",
    "redirect_uris": [
      "https://localhost:44378/authorize/",
      "https://localhost:44378/authorize"
    ],
    "javascript_origins": [
      "https://developers.google.com"
    ]
  }
}

The code I use to create the Google service is quite closely copied from their .NET Quickstart (step 2)...

UserCredential credential;
using (FileStream stream = new(credJson, FileMode.Open, FileAccess.Read)) {
  credential = GoogleWebAuthorizationBroker
    .AuthorizeAsync(GoogleClientSecrets.FromStream(stream).Secrets, 
      new[] { DriveService.Scope.Drive }, 
      "user", 
      CancellationToken.None, 
      new FileDataStore(tokenPath, true))
    .Result;
}
DriveService service = new(new BaseClientService.Initializer {
  HttpClientInitializer = credential,
  ApplicationName = ""
});

That all worked fine. Now the client has set up another Google account specially for this site, and wants me to use that instead. No problem, I went to the Google developer console for the new account and did the same as before, ie configured the OAuth screen, created a new OAuth credential, downloaded the JSON file, added the redirect URIs as shown above, and replaced the existing JSON file with the new one.

When I try to debug, I get the error shown in the title.

Having read loads of other questions about this error, I have checked and double-checked the following...

  • The URI that VS uses never changes. Specifically, the port is always 44378
  • The base URI in the Google console and in my JSON file is the exact URI in the browser address bar when I'm debugging, I just added /authorize with and without the trailing slash
  • The URI that the Google window shows is of the form http://127.0.0.1:59777/authorize/, although the port number changes every time. It is not picking up the port that VS uses. Adding the one Google shows is a waste of time, as next time I try, it shows a different port
  • I have tried using 127.0.0.1 instead of localhost
  • I have tried removing the port altogether from the redirect URI
  • I have tried setting the redirect URIs in both the Google console (and waiting for them to update in case this was needed) and in the JSON file
  • When I created the credentials, I set is as a web project, not a desktop one

None of this helped, I always get the same error.

The weird thing is that if I now revert my credentials JSON file back to what it was before (with the client's own Google account), I get the same error.

Anyone any ideas? As far as I can see, I've tried everything mentioned in other answers, but still get this error.

1 Answers

Hmm, reading your (very clear by the way!) question gives me the impression that all the changes you made were in the JSON file, not in the Google console. Correct me if I'm wrong, but I think that could be where you're hitting issues.

My (admittedly limited) experience leads me to believe that the redirect URIs in the JSON file are ignored, and it's the one(s) in the Google console that are used.

Forgive me if you already tried this, but try adding the following four URIs to your console, and then downloading the JSON file again...

http://127.0.0.1/authorize/
http://127.0.0.1/authorize
https://127.0.0.1/authorize/
https://127.0.0.1/authorize

As you've realised, you seem to need to include versions with and without a trailing slash. I've found you often need to include the non-SSL versions as well. Don't ask me why, as VS uses https when debugging, but this is Google!

Once you've updated the console, download the JSON and include the new version in your app.

Anyway, try that and see if it works.

Related