我正在尝试使用服务器端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)并使用令牌和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);
}
})
}
答案 0 :(得分:0)
如果你在Google游乐场进行测试并且看起来都很好,我会开始考虑你正在使用的其他外部依赖项。例如,请求。也许你需要传入一个解析的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
答案 1 :(得分:0)
您只需传递raw
中的body
消息:
function sendMail (oauth2token, raw) {
var options = {
method: 'POST',
url: 'https://www.googleapis.com/gmail/v1/users/me/messages/send',
headers: {
'HTTP-Version': 'HTTP/1.1',
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + oauth2token
},
body: JSON.stringify({
raw: raw
})
};
request(options, function (error, response, body) {
if (!error && response.statusCode == 200) {
context.log(body);
}
if (error) {
context.log(error);
} else {
context.log(response);
}
});
}