问题描述
我正尝试使用Google的API发送电子邮件。我有一个由AngularJS支持的Web应用程序,用户使用oauth2使用他们的Google帐户(通过passport.js)登录。一个新的访问令牌被写入我的数据库的帐户。他们的谷歌用户ID也写入他们的帐户。我希望用户能够使用简单的用户ID和访问令牌通过HTTP请求发送电子邮件。我使用Postman来提出一些测试请求,但是我一直在收到这个错误:
上述需求必须存在于您发送邮件的 POST - 请求中。 -procotol表明你需要传递一个头< Authorization:Bearer < ACCESS_TOKEN> 或一个参数 access_token =< ACCESS_TOKEN> 。 p
raw 的值也需要是有效的邮件。在JavaScript中的一个例子可能如下所示:
// Base64对邮件进行编码并使其成为URL安全的
/(用 - 替换所有的+,用_替换所有的/)
var encodedMail = btoa(
Content-Type:text / plain; charset = \ UTF-8 \\\\
+
MIME-Version:1.0 \\\
+
Content-Transfer-Encoding:7bit \\\
+
Subject:邮件主题\\\
+
发件人:[email protected]\\\
+
收件人:[email protected]\\\
\\\
+
这就是邮件文本的位置
).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/sendIn my header, I have:
Authorization: Bearer yaxx._wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Content-Type: application/jsonMy 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: [email protected]\n" + "To: [email protected]\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的电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!