1
votes

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("[email protected]");
    mail.IsBodyHtml = true;

    mail.To.Add(new MailAddress("[email protected]"));
    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#.

1
Welcome to StackOverflow Looks like the error is pretty self-explanatory. Did you add the redirect to Google? - HazardousGlitch
Welcome to stack please edit your question and include your code, make sure you add 127.0.0.1:55878/authorize as a redirect uri in Google developer console for your project. - DaImTo
I have added localhost:44381/Default.aspx as a redirect uri in Google developer console. - jksutaria
GoogleWebAuthorizationBroker.AuthorizeAsync is for installed applications you need to use developers.google.com/api-client-library/dotnet/guide/… for a web application. The redirect uri used by the client library is as follows 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? - DaImTo
I got in comment in googling for filedatastore to null so - jksutaria

1 Answers

0
votes

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.