Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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#.

like image 556
jksutaria Avatar asked Jan 01 '26 08:01

jksutaria


1 Answers

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.

like image 184
DaImTo Avatar answered Jan 03 '26 09:01

DaImTo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!