我正在尝试使用服务器端 node.js 向 Gmail Send Message API 发送请求但没有成功。我收到以下错误:
body: '{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalidArgument",
"message": "\'raw\' RFC822 payload
message string or uploading message via /upload/ URL required"
}
],
"code": 400,
"message": "\'raw\' RFC822 payload message
string or uploading message via /upload/ URL required"
}
}'
}
oauth2token 和 raw 的输入参数是有效的,事实上,如果我使用 Google OAuth 2 playground( https://developers.google.com/oauthplayground )并使用 token 和 raw 作为电子邮件发送的值。有人能看到我错过了什么吗?
function sendMail(oauth2token, raw) {
context.log('Token: ' + oauth2token);
context.log('raw: ' + raw);
var params = {
userId: user_id,
resource: { 'raw': raw}
};
var headers = {
"HTTP-Version": "HTTP/1.1",
"Content-Type": "application/json",
"Authorization": "Bearer " + oauth2token
};
var options = {
headers: headers,
url: "https://www.googleapis.com/gmail/v1/users/me/messages/send",
method: "POST",
params: params
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
context.log(body);
}
if (error) {
context.log(error);
}
else {
context.log(response);
}
})
}
最佳答案
如果您在 Google Playground 中进行测试并且一切看起来都不错,我会开始查看您正在使用的其他外部依赖项。例如,请求。也许你需要传入一个解析过的 url 对象而不是 url。看看这个:https://github.com/request/request#requestoptions-callback .
这是您解析的 url 对象的样子:
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.googleapis.com',
port: null,
hostname: 'www.googleapis.com',
hash: null,
search: null,
query: null,
pathname: '/gmail/v1/users/me/messages/send',
path: '/gmail/v1/users/me/messages/send',
href: 'https://www.googleapis.com/gmail/v1/users/me/messages/send' }
要么将您现有的选项更改为 uri 而不是 url
关于Node.js POST 请求到 Gmail API 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46122770/