I am trying to send emails from my application using my Gmail account with Ionic. I have followed tutorials from SitePoint and Google Developers. Here is how I'm initializing the client:
client_id: gapiKeys.client_id,
discoveryDocs: ["https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest"],
scope: [
"https://www.googleapis.com/auth/gmail.readonly",
// other scopes here
].join(" ")
When I only include "" in the scope, I can retrieve user labels, but including mail.google.com, modify, compose, and send gives me a 401 error. Below is my email sending code:
sendEmail() {
let top = {
'To': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="23504c4e467146404a534a464d5763444e424a4f0d404c4e">[email protected]</a>',
'Subject': 'Test'
}
var email = '';
for(var header in top) {
email += header += ": "+ top[header] + "\r\n";
}
email += "\r\n" + "this is a testing email message.";
var request = (gapi.client as any).gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': window.btoa(email).replace(/\+/g, '-').replace(/\//g, '_')
}
});
request.execute();
}
The error response looks like this:
POST https://content.googleapis.com/gmail/v1/users/me/messages/send?alt=json&key=someKey--M 401 ()
The error details show the following:
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
How can I successfully send emails from my Ionic application without using the Gmail API's email composer?