Ok, so I'm trying to implement Gmail API on this project, everything I want to do is send an email, this is what I got so far:
In consent screen I just changed app name and scopes for gmail.send.
Create my credentials as a webapp and this what I put on redirect uris.
As I'm working with MVC and wanted to use the template with Home controller and Index View, I don't know if this is correct but as far as I know, I must write the page where it should go after the authentication and that's the only page in my project.
Then I download the credentials.json file and add it to my project, then execute this code, ruta is the path to my credentials.json
UserCredential credential;
using (var stream =
new FileStream(ruta, FileMode.Open, FileAccess.Read))
{
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None).Result;
}
After that I execute the program but I never see the auth page, instead of that, comes the error:
Error 400: redirect_uri_mismatch. The redirect URI in the request, http://127.0.0.1:62297/authorize/, does not match the ones authorized for the OAuth client.
I've tried to add that URI as "http://localhost/authorize" and "http://localhost/authorize/" to my redirect URIs but I keep getting the same error message.
I'm pretty newbie with this, so I'm a bit lost about all this redirect uris, thanks for your answers
The redirect uri in google developer console must exactly match the one you are sending from. It basically tells Google's authorization server where you would like the
Error 400: redirect_uri_mismatch. The redirect URI in the request, http://127.0.0.1:62297/authorize/, does not match the ones authorized for the OAuth client.
Means that you are sending from http://127.0.0.1:62297/authorize/ and have not added this Port in your google developer console for that project. credentials returned to.
Video showing how to fix this: How the fix redirect_uri_mismatch error. Part 2
If the port number is changing this is an issue with your development environment on your project you need to set the project up to use a static port that you can add to Google developer console.
You state in your question that you are using MVC how ever the code you are using GoogleWebAuthorizationBroker.AuthorizeAsync
is designed to work with installed applications. When it runs it will launch the consent window browser on the machine that its running off. THis will not work with a web application as it will attempt to launch a browser on the server which it doesnot have access to do.
Instead use GoogleAuthorizationCodeFlow