使用仅访问令牌的 Google API 发送电子邮件
Send email using Google API with only access token
我想通过 Google API 发送一封没有不必要的 OAUTH2 参数的电子邮件。我只有那个用户的 access_token 和 refresh_token。
如何使用 Request npm 插件在 NodeJS 中通过基本 POST 请求通过 Gmail API 发送电子邮件?
有 two methods 用于将 OAuth2 access_tokens 附加到 Google API 请求。
- 像这样使用 access_token 查询参数:
?access_token=oauth2-token
- 像这样使用 HTTP 授权 header:
Authorization: Bearer oauth2-token
第二个是 POST 请求的首选,因此发送电子邮件的原始 HTTP 请求看起来像这样。
POST /gmail/v1/users/me/messages/send HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer oauth2Token
{"raw":"encodedMessage"}
abraham 是正确的,但我只是想给你举个例子。
var request = require('request');
server.listen(3000, function () {
console.log('%s listening at %s', server.name, server.url);
// Base64-encode the mail and make it URL-safe
// (replace all "+" with "-" and all "/" with "_")
var encodedMail = new Buffer(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"to: reciever@gmail.com\n" +
"from: sender@gmail.com\n" +
"subject: Subject Text\n\n" +
"The actual message text goes here"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
request({
method: "POST",
uri: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
headers: {
"Authorization": "Bearer 'access_token'",
"Content-Type": "application/json"
},
body: JSON.stringify({
"raw": encodedMail
})
},
function(err, response, body) {
if(err){
console.log(err); // Failure
} else {
console.log(body); // Success!
}
});
});
不要忘记更改收件人和发件人的电子邮件地址以使示例正常工作。
我想通过 Google API 发送一封没有不必要的 OAUTH2 参数的电子邮件。我只有那个用户的 access_token 和 refresh_token。
如何使用 Request npm 插件在 NodeJS 中通过基本 POST 请求通过 Gmail API 发送电子邮件?
有 two methods 用于将 OAuth2 access_tokens 附加到 Google API 请求。
- 像这样使用 access_token 查询参数:
?access_token=oauth2-token
- 像这样使用 HTTP 授权 header:
Authorization: Bearer oauth2-token
第二个是 POST 请求的首选,因此发送电子邮件的原始 HTTP 请求看起来像这样。
POST /gmail/v1/users/me/messages/send HTTP/1.1
Host: www.googleapis.com
Authorization: Bearer oauth2Token
{"raw":"encodedMessage"}
abraham 是正确的,但我只是想给你举个例子。
var request = require('request');
server.listen(3000, function () {
console.log('%s listening at %s', server.name, server.url);
// Base64-encode the mail and make it URL-safe
// (replace all "+" with "-" and all "/" with "_")
var encodedMail = new Buffer(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"to: reciever@gmail.com\n" +
"from: sender@gmail.com\n" +
"subject: Subject Text\n\n" +
"The actual message text goes here"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
request({
method: "POST",
uri: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
headers: {
"Authorization": "Bearer 'access_token'",
"Content-Type": "application/json"
},
body: JSON.stringify({
"raw": encodedMail
})
},
function(err, response, body) {
if(err){
console.log(err); // Failure
} else {
console.log(body); // Success!
}
});
});
不要忘记更改收件人和发件人的电子邮件地址以使示例正常工作。