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

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

问题描述

我正尝试使用Google的API发送电子邮件。我有一个由AngularJS支持的Web应用程序,用户使用oauth2使用他们的Google帐户(通过passport.js)登录。一个新的访问令牌被写入我的数据库的帐户。他们的谷歌用户ID也写入他们的帐户。我希望用户能够使用简单的用户ID和访问令牌通过HTTP请求发送电子邮件。我使用Postman来提出一些测试请求,但是我一直在收到这个错误:

  {
error :{
errors:[
{
domain:global,
reason:insufficientPermissions,
message:Insufficient权限
}
],
code:403,
message:权限不足
}
}

我使用以下链接发送POST请求:

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

在我的头文件中,我有:

 授权:持票人yaxx._wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
Content-Type:application / json

我的身体:

  {
raw:test
}

我有一些电子邮件间歇性地使用这种方法,但我不能似乎可以肯定地重新创建一个成功的请求。我对Google的文档有点困惑。我是否需要明确授予访问权限,如

上述需求必须存在于您发送邮件的 POST - 请求中。 Oauth2 -procotol表明你需要传递一个头< Authorization:Bearer < ACCESS_TOKEN> 或一个参数 access_token =< ACCESS_TOKEN> p

raw 的值也需要是有效的 base64-encoded rfc822 邮件。在JavaScript中的一个例子可能如下所示:

  // Base64对邮件进行编码并使其成为URL安全的
/(用 - 替换所有的+,用_替换所有的/)
var encodedMail = btoa(
Content-Type:text / plain; charset = \ UTF-8 \\\\
+
MIME-Version:1.0 \\\
+
Content-Transfer-Encoding:7bit \\\
+
Subject:邮件主题\\\
+
发件人:sender@gmail.com\\\
+
收件人:reciever@gmail.com\\\
\\\
+

这就是邮件文本的位置
).replace(/ \ + / g,' - ')。replace(/ \ // g,'_');

这会产生一个字符串,用作 raw

Postman中的请求将如下所示:

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

{//上面例子中的编码邮件。
原始: Q29udGVudC1UeXBlOiB0ZXh0L3BsYWluOyBjaGFyc2V0PSJVVEYtOCIKTUlNRS1WZXJzaW9uOiAxLjAKQ29udGVudC1UcmFuc2Zlci1FbmNvZGluZzogN2JpdApTdWJqZWN0OiBTdWJqZWN0IG9mIHRoZSBtYWlsCkZyb206IHNlbmRlckBnbWFpbC5jb20KVG86IHJlY2lldmVyQGdtYWlsLmNvbQoKVGhpcyBpcyB3aGVyZSB0aGUgbWFpbCB0ZXh0IHdpbGwgZ28 =
}

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



另外,请确保您要求提供对Passport有足够的权限 scope https://mail.google.com/ 可以使用。


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"
  }
}

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

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

In my header, I have:

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

My body:

{
 "raw": "test"
}

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?

解决方案

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>.

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, '_');

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="
}

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.

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

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

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