0

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:

  1. Register a new project at google developers console, enable the gmail API and configure the consent screen.
  2. In consent screen I just changed app name and scopes for gmail.send.

  3. Create my credentials as a webapp and this what I put on redirect uris.

http://localhost/Home/Index

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Juan Herrera
  • 3
  • 1
  • 1
  • 2
  • why not add '127.0.0.1:6666/authorize' to the list – pm100 Jun 14 '20 at 22:52
  • The port number change everytime I run the program and I saw that it actually doesn't matter, I've just tried changing all my "localhost" URIs to "127.0.0.1" and it worked, now I need to see which one is the one that worked but thanks for that – Juan Herrera Jun 14 '20 at 23:05

2 Answers2

4

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

static port needed.

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.

MVC vs installed application

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

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
  • About the GoogleWebAuthorizationBroker, I want to send the email from a static gmail account provided by me, so I want to authorize it from server and not ask users for any authentication as they will not use their mail at all, they'll just provide the destination mail Is it correct wat I'm trying to do? – Juan Herrera Jun 15 '20 at 21:19
  • Unless this is a gsuite domain you cant use service accounts to authorize your request. Your going to have to use Oauth2 the issue with this will be that it needs to be preauthorized or its going to request a users access to their gmail account. To do that you will need to save the credentials file that was created and use that to send your mails. IF your doing this with MCV i have no idea how your going to achive that. You could try creating a custom idatastore and over ride file datastore with it. but if your using ASP .net core i dont think thats going to work. – Linda Lawton - DaImTo Jun 16 '20 at 06:01
  • "use a static port" HOW? – Heinzlmaen Mar 30 '21 at 07:40
  • That depends on the IDE you are using. [visual studio](https://stackoverflow.com/a/1126560/1841839) – Linda Lawton - DaImTo Mar 30 '21 at 09:15
1

Here is example that work for me. Authorised JavaScript origins: 127.0.0.1 Authorised redirect URIs: 127.0.0.1/oauth/complete/google-oauth2 We should not use any port number after main url. Remove the port number. That's all. Go here & correct the redirect urls: `

https://console.cloud.google.com/apis/credentials/oauthclient/

`

abdulalim
  • 430
  • 5
  • 9