[英]Use Gmail API to send mail in a Chrome App
I am trying to create a Chrome app and I want to send email . 我正在尝试创建一个Chrome应用,并且想发送电子邮件 。 I found this , but I can't figure out how make it work.
我找到了 ,但是我不知道它是如何工作的。
function sendMessage(userId, to, subject, email) {
authUser(function() {
var base64EncodedEmail = btoa(email);
var request = gapi.client.gmail.users.messages.send({
'userId': userId,
'message': {
'raw': base64EncodedEmail,
'headers': [
{'To': to},
{'Subject': subject}
]
}
});
request.execute();
});
}
function authUser(callback) {
chrome.identity.getAuthToken({'interactive': true}, function(token) {
// load Google's javascript client libraries
var url = "https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=" + token;
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if (request.readyState !== 4 || request.status !== 200) {
return;
}
var response = JSON.parse(request.responseText);
console.log(response);
callback();
}
;
request.open('POST', url, true);
request.send();
request.setRequestHeader('Authorization', 'Bearer ' + token);
});
}
I put the code in my background.js (call sendMessage on launch), and get the following manifest.json : 我将代码放在background.js中 (在启动时调用sendMessage),并获得以下manifest.json :
{
"name": "Name",
"description": "Description",
"permissions": [
"storage",
{ "fileSystem": ["write"] },
"identity",
"https://mail.google.com/"],
"version": "1.0",
"manifest_version": 2,
"oauth2": {
"client_id": "XXXXX.apps.googleusercontent.com",
"scopes": [
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/gmail.send"
]
},
"app": {
"background": {
"scripts": [
"background.js"]
}
},
"icons": { "16": "images/mailling-16.png", "128": "images/mailling-128.png" }
}
So, I understand that we need to establish the connection (with our credentials) with Gmail API and then we send the mail . 因此,我知道我们需要与Gmail API建立连接(使用凭据),然后发送邮件 。 But I am having HTTP error 400 on the POST request and server throw : 'raw' RFC822 payload message string or uploading message via /upload/* URL required
但是我在POST请求和服务器抛出HTTP错误400 : '原始'RFC822有效负载消息字符串或通过/ upload / * URL上载消息
I don't see why he is expecting a message, whereas we are establishing the connection :I 我看不到他为什么要收到消息,而我们正在建立连接:I
My best solution is to switch to google script apps , you can send mail in only one with MailApp.sendEmail . 我最好的解决方案是切换到Google脚本应用 ,您只能使用MailApp.sendEmail发送邮件。
Anyway, that's not a solution for Chrome app. 无论如何,这不是Chrome应用程序的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.