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

查看:17
本文介绍了仅使用访问令牌使用 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.

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

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"
" +
        "MIME-Version: 1.0
" +
        "Content-Transfer-Encoding: 7bit
" +
        "to: reciever@gmail.com
" +
        "from: sender@gmail.com
" +
        "subject: Subject Text

" +

        "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天全站免登陆