I am completely going mad trying to get this work. I need to use the gmail api to send mail from our domain. Every time I get this result back:
Google.Apis.Requests.RequestError
Bad Request [400]
Errors [
Message[Bad Request] Location[ - ] Reason[failedPrecondition] Domain[global]
]
I'm going completely mad!
I've read all of this and I still did not figure it out:
- https://developers.google.com/admin-sdk/reports/v1/guides/delegation
- https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#service-account
- Can we access GMAIL API using Service Account?
- Gmail REST API : 400 Bad Request + Failed Precondition
How do I get this to work?
These are the steps I have done:
- Created a new project in console.developers.google.com
- Enabled the gmail api
- Created a service account
- Created a key.p12 file for the service account
- Configured domain-wide authority
- Enabled the gmail api in admin.google.com
- Added the api scopes in security->advanced->Api acces
- Added and verified my domain name
Screens:
This is my code:
public class TestMailController : Controller
{
static string ApplicationName = "IdiciumGmail";
private static String serviceAccountEmail = "[email protected]";
public IActionResult Index()
{
string debugMessage = "";
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new[] { GmailService.Scope.MailGoogleCom }
}.FromCertificate(certificate));
if (credential.RequestAccessTokenAsync(CancellationToken.None).Result)
{
// Create Gmail API service.
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
/*
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Bestuur", "[email protected]"));
message.To.Add(new MailboxAddress("Tester", "[email protected]"));
message.Subject = "Is this working";
message.Body = new TextPart(TextFormat.Html)
{
Text = "Ja ja als je dit krijgt werkt het.... EINDELIJK. <br><br> <b>ook html werkt</b>"
};
*/
Message m = new Message();
m.Raw = "RnJvbTogQmVzdHV1ciA8YmVzdHV1ckBpbmRpY2l1bS5odT4gClRvOiBXaWxsaWFtIExvb3NtYW4g\nPHdpbGxpYW0ud2xAbGl2ZS5ubD4gClN1YmplY3Q6IERpdCBpcyBlZW4gdGVzdAoKVGhpcyBpcyBh\nIG1lc3NhZ2UganVzdCB0byBzYXkgaGVsbG8uIFNvLCAiSGVsbG8iLg==";
try
{
//var result = service.Users.Messages.Send(m, "[email protected]").Execute();
var result = service.Users.Messages.List("[email protected]").Execute();
debugMessage += "Send email: " + result;
}
catch (Exception e)
{
debugMessage += e.Message;
//throw;
}
debugMessage += "The END!";
}
return Content(debugMessage);
}
I tested the base 64 raw string inside the api explorer and there it worked! So I guess that is not the problem. Can someone please help me with this, I'm getting desperate. BTW we use the free version of G suite for nonprofits (does this form a problem?).