只使用访问令牌,使用Google API发送电子邮件 [英] Send email using Google API with only access token

查看:93
本文介绍了只使用访问令牌,使用Google API发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Google API发送电子邮件,而不使用不必要的OAUTH2参数。我只有该用户的access_token和refresh_token。

I want to send an email through Google API without the unnecessary OAUTH2 parameters. I only have the access_token and the refresh_token of that user.

如何通过NodeJS中的基本POST请求通过Gmail API发送电子邮件,请求npm插件?

How can I send an email through Gmail API through a basic POST request in NodeJS, with Request npm plugin?

推荐答案

abraham是正确的,但我只是想给你一个例子。

abraham is correct, but I just thought I'd give you an example.

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发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆