仅使用令牌发送带有Gmail API的电子邮件? [英] Sending an email with Gmail's API using only a token?

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

问题描述

我正在尝试使用Google的API发送电子邮件.我有一个由AngularJS驱动的Web应用程序,其中用户使用oauth2使用其Google帐户(通过passport.js)登录.一个新的访问令牌将写入他们在我的数据库中的帐户.他们的google用户ID也将写入他们的帐户.我希望用户能够仅使用其用户ID和访问令牌通过HTTP请求发送电子邮件.我正在使用Postman发出一些测试请求,但我不断收到此错误:

I'm trying to using Google's API to send an email. I have a web application powered by AngularJS where the user signs in with their google account (via passport.js) using oauth2. A new access token is written to their account on my database. Their google user ID is also written to their account. I'd like the user to be able to send an email via an HTTP request using simply their user Id and access token. I'm using Postman to make some test requests, but I keep getting this error:

{
  "error": {
    "errors": [
      {
        "domain": "global",
        "reason": "insufficientPermissions",
        "message": "Insufficient Permission"
      }
    ],
    "code": 403,
    "message": "Insufficient Permission"
  }
}

我正在使用以下链接发出POST请求:

I'm using the following link to make a POST request:

https://content.googleapis.com/gmail/v1/users/106xxxxxxxxxxx/messages/send

在标题中,我有:

Authorization: Bearer yaxx._wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Content-Type: application/json

我的身体:

{
 "raw": "test"
}

我使用这种方法间歇性地收到了一些电子邮件,但是我似乎无法确定地重新创建成功的请求.Google的文档让我有些困惑.我是否需要显式授予访问权限,如此页面?

I've had some of the emails intermittently come in using this method, but I can't seem to recreate a successful request with certainty. I'm a little confused by Google's documentation. Do I need to explicitly grant access as seen in the example at the bottom of this page?

推荐答案

您提到的访问令牌必须出现在 POST -请求中,并通过该请求发送邮件. Oauth2 -procotol指示您需要传递标头 Authorization:Bearer< ACCESS_TOKEN> 或参数 access_token =< ACCESS_TOKEN> .

The access token you mentioned needs to be present in the POST-request you send the mail with. The Oauth2-procotol dictates that you need to either pass along a header Authorization: Bearer <ACCESS_TOKEN>, or a parameter access_token=<ACCESS_TOKEN>.

raw 的值也必须是有效的

The value of raw also needs to be a valid base64-encoded rfc822 mail. An example in JavaScript could look like the following:

// Base64-encode the mail and make it URL-safe 
// (replace all "+" with "-" and all "/" with "_")
var encodedMail = btoa(
      "Content-Type: text/plain; charset=\"UTF-8\"\n" +
      "MIME-Version: 1.0\n" +
      "Content-Transfer-Encoding: 7bit\n" +
      "Subject: Subject of the mail\n" +
      "From: sender@gmail.com\n" +
      "To: reciever@gmail.com\n\n" +

      "This is where the mail text will go"
    ).replace(/\+/g, '-').replace(/\//g, '_');

这将导致您在请求正文中将其用作 raw 的字符串.

This will result in a string that you use as the raw in the request body.

邮递员中的请求如下所示:

A request in Postman would then look like the following:

POST https://www.googleapis.com/gmail/v1/users/me/messages/send?access_token=<ACCESS_TOKEN>

{ // The encoded mail from the example above.
 "raw": "Q29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzogN2JpdApTdWJqZWN0OiBTdWJqZWN0IG9mIHRoZSBtYWlsCkZyb206IHNlbmRlckBnbWFpbC5jb20KVG86IHJlY2lldmVyQGdtYWlsLmNvbQoKVGhpcyBpcyB3aGVyZSB0aGUgbWFpbCB0ZXh0IHdpbGwgZ28="
}

您还需要向 Content-Type 标题提供值 application/json .请注意,您不必使用userId.提供 me 将使Google自动使用与提供的访问令牌关联的用户.

You also need to supply the Content-Type-header with the value of application/json. Note that you don't have to use a userId. Supplying me will make Google use the user associated with the supplied access token automatically.

还请确保您要求Passport拥有足够的权限 scope . https://mail.google.com/可以正常工作.

Also make sure you asked for a sufficient permission scope with Passport. https://mail.google.com/ will work.

这篇关于仅使用令牌发送带有Gmail API的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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