3

I am trying to send email using google API in asp.net c# and getting Error 400 redirect_uri_mismatch

below is my code:

public void sendemail()
{
    static string[] Scopes = { GmailService.Scope.GmailSend};
    static string ApplicationName = "GmailAPIWeb";

    string CLIENT_ID = "***********************";
    string CLIENT_SECRET = "************";

    var secrets = new ClientSecrets
    {
        ClientId = CLIENT_ID,
        ClientSecret = CLIENT_SECRET
    };

    //Error will throw from here...
    var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        secrets,
        Scopes, "user",
        CancellationToken.None, null
         //new FileDataStore(credPath, true)
        ).Result;

    var service = new GmailService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = ApplicationName,
    });

    //Create Message`enter code here`
    MailMessage mail = new MailMessage();
    mail.Subject = "Subject!";
    mail.Body = "This is <b><i>body</i></b> of message";
    mail.From = new MailAddress("jayesh@gmail.com");
    mail.IsBodyHtml = true;

    mail.To.Add(new MailAddress("jayesh@gmail.com"));
    MimeKit.MimeMessage mimeMessage = 
    MimeKit.MimeMessage.CreateFromMailMessage(mail);

    var gmailMessage = new Google.Apis.Gmail.v1.Data.Message
    {
        Raw = Encode(mimeMessage.ToString())
    };

    //Send Email
    var result = service.Users.Messages.Send(gmailMessage, "me/OR UserId/EmailAddress").Execute();
}

The redirect URI in the request, http://127.0.0.1:55878/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

I want send email using google api using c#.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
jksutaria
  • 31
  • 2
  • 2
    Welcome to StackOverflow Looks like the error is pretty self-explanatory. Did you add the redirect to Google? – HazardousGlitch Oct 08 '20 at 11:58
  • 1
    Welcome to stack please edit your question and include your code, make sure you add http://127.0.0.1:55878/authorize/ as a redirect uri in Google developer console for your project. – Linda Lawton - DaImTo Oct 08 '20 at 12:11
  • 1
    I have added https://localhost:44381/Default.aspx as a redirect uri in Google developer console. – jksutaria Oct 08 '20 at 12:29
  • 2
    GoogleWebAuthorizationBroker.AuthorizeAsync is for installed applications you need to use https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-asp.net-mvc for a web application. The redirect uri used by the client library is as follows http://127.0.0.1:55878/authorize/ you cant just set it to whatever. You need to add the one the client library is using to google developer console. why would you set filedatastore to null? – Linda Lawton - DaImTo Oct 08 '20 at 12:51
  • 1
    I got in comment in googling for filedatastore to null so – jksutaria Oct 08 '20 at 13:03
  • 1
    Can you provide me steps or examples to send email using gmail api ?. – jksutaria Oct 08 '20 at 13:04
  • 1
    Just follow the [this guide](https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web-applications-asp.net-mvc), while changing the parts related to the `DriveService` in the code samples to the corresponding `GmailService` ones (see [library docs](https://googleapis.dev/dotnet/Google.Apis.Gmail.v1/latest/api/Google.Apis.Gmail.v1.html)). – Iamblichus Oct 09 '20 at 11:27
  • 1
    If you want to send messages, you have to use [users.messages.send](https://developers.google.com/gmail/api/reference/rest/v1/users.messages/send) ([Class UsersResource.MessagesResource.SendRequest](https://googleapis.dev/dotnet/Google.Apis.Gmail.v1/latest/api/Google.Apis.Gmail.v1.UsersResource.MessagesResource.SendRequest.html)). – Iamblichus Oct 09 '20 at 11:27
  • 1
    About your original question: [Google OAuth 2 authorization - Error: redirect\_uri\_mismatch](https://stackoverflow.com/questions/11485271/google-oauth-2-authorization-error-redirect-uri-mismatch) – Iamblichus Oct 09 '20 at 11:32
  • 1
    Thank you for your reply – jksutaria Oct 12 '20 at 06:43

1 Answers1

1

The redirect URI in the request, http://127.0.0.1:55878/authorize/, does not match the ones authorized for the OAuth client. To update the authorized redirect URIs, visit: https://console.developers.google.com/apis/credentials/oauthclient/${your_client_id}?project=${your_project_number}

Means exactly that. The redirect uri you are sending from your application http://127.0.0.1:55878/authorize/ must exactly match one that you have set in Google developer console

Web application vs installed application.

GoogleWebAuthorizationBroker.AuthorizeAsync is for installed applications, it is designed to open the consent screen on the machine the code is running on. This will not work then if you want to host it on a webserver. What you need to use Web applications (ASP.NET MVC)for a web application.

FileDataStore null

This really doesn't matter as you shouldn't be using GoogleWebAuthorizationBroker.AuthorizeAsync by setting Idatastore null you are just telling it to use FileDataStore which is the default data store the installed application client will use to store credentials

//new FileDataStore(credPath, true)

verification

You might want to look into verification of your application early it can take a while to get an application with gmail scopes verified though google as its done though a third party company and is quite expensive.

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449