public string ConvertToBase64(string text)
{
return Convert.ToBase64String(Encoding.UTF8.GetBytes(text))
.Replace("+", "-")
.Replace("/", "_")
.Replace("=", "");
}
public async Task<ResponseResult> SendEmails(string reciepient_email, string GUID)
{
var client_id = _config["SMTP:client_id"];
var client_secret = _config["SMTP:client_secret"];
if (client_id == null || client_secret == null)
{
return new ResponseResult(false, "Unable to fetch token");
}
string[] scopes = { GmailService.Scope.GmailSend };
var redirectURI = GoogleAuthConsts.LocalhostRedirectUri;
var oauthCredentials = GoogleWebAuthorizationBroker.AuthorizeAsync(
new ClientSecrets
{
ClientId = client_id,
ClientSecret = client_secret
}, scopes, "user", CancellationToken.None).Result;
var access_token = await oauthCredentials.GetAccessTokenForRequestAsync();
var gmail_access_token = GoogleCredential.FromAccessToken(access_token)
.CreateScoped(GmailService.Scope.GmailSend);
var gmailService = new GmailService(new BaseClientService.Initializer
{
HttpClientInitializer = gmail_access_token,
});
var emailMessage = new MimeMessage();
emailMessage.From.Add(new MailboxAddress("",_config["SMTP:from"]));
emailMessage.To.Add(new MailboxAddress("", reciepient_email));
emailMessage.Subject = "Invite to Prescreening Test";
emailMessage.Body = new TextPart(MimeKit.Text.TextFormat.Html) { Text= "WALLAHI LUIGI" };
var gmailMessage = new Message
{
Raw = ConvertToBase64(emailMessage.ToString()),
};
var result = await gmailService.Users.Messages.Send(gmailMessage, "user").ExecuteAsync();
return new ResponseResult(false, "Error with emailing service");
}
I’m using the above code to try and send an email using the google gmail api using .net core 6
But i get a Error 400: redirect_uri_mismatch
, with additional details of Request details: redirect_uri=http://127.0.0.1:49747/authorize/ flowName=GeneralOAuthFlow
https://i.sstatic.net/MIICCjpB.png
I’ve added these as the redirect uris in my oauth key (type web application)
Considering what other answers have recommended, I have added both http and https variants but it still doesn’t work
Any recommendations on how I should change my code to get this to work
1