I wanted to upload files to Google Drive using Google Drive API in ASP.NET MVC C#. While doing that I have followed the below URL: https://qawithexperts.com/article/asp-net/upload-file-to-google-drive-using-google-drive-api-in-aspnet/236
While implementing the code after calling my API google redirect to an error page showing
Error 400: redirect_uri_mismatch
I want to clarify that I have mentioned the right/correct URL as mentioned in the project properties please look into my issue.
I am attaching the screenshot which contains the redirect_uri.
CodePudding user response:
The redirect uri must exactly match the one you have registered in Google developer console
If we look at your image you have the following two redirect uris registered
- http://localhost:60581/
- http://localhost:60581/api/TwilioWP/CallGoogleAPI
This means that Google will accept only those two redirect uris from your application. The test is an exact match.
Now if we look at the error message we can see that your application is sending
As you can see this does not match the ones you have registered. What you should do is copy that one and add it.
Note if your ide is changing ports you need to set it to a static port or its not going to work. It needs to match exactly
Google OAuth2: How the fix redirect_uri_mismatch error. Part 2 server sided web applications
Your reall issue.
The tutorial you are following uses GoogleWebAuthorizationBroker.AuthorizeAsync
This method is designed for use with an installed application. So in fact the credentials and client type you are using is wrong. If you want to use that code you need to create native or desktop client.
If you try to host this as it is it will attempt to open the web browser on the server. When what you need to do is open the web browser for user consent on the users machine. You should be following web-applications-asp.net-core-3 if you want to be able to authorize a user using your web application.
I offer you my tutorial on Asp .net core 3 and Google login which has a link to the YouTube video on the same topic.
So the real cause of your problem is that you are using code for an installed application which has a default redirect Uri of http://127.0.0.1:{Port}/authorize/
and you are trying to use this for a web application. Which will need to have the host set to that of your web domain.
I have been in touch with the author of the tutorial you are following. I suspect they will be addressing the issue soon. Twitter thread