我试图通过Google API和Axios发送一封新电子邮件:
async SendMail (req, res) {
try {
const email = `From: ${req.body.sender}\r\n` + `To: ${req.body.receiver}\r\n` + `Subject: ${req.body.subject}\r\n\r\n` + req.body.message
const response = await axios.post(`https://www.googleapis.com/upload/gmail/v1/users/${req.body.userId}/messages/send`, email, {headers: {Authorization: `Bearer ${req.body.token}`, 'content-type': 'message/rfc822'}})
res.status(200).send({data: response.data, status: 'ok'})
} catch(err) {
console.log('error => ', err)
res.status(404).send({
error: 'Error for the recuperation of Google info'
})
}
}
它告诉我我未经授权,但是我添加了正确的范围。
error: {
response: {
status: 401,
statusText: 'Forbidden',
...
}
}
accessToken正常工作,我已经用它进行了一次调用,效果很好。
这是我的范围:https://mail.google.com/