const getAccessToken = async () => {
let data = qs.stringify({
'client_id': config.EMAIL_CLIENT_ID,
'scope': config.EMAIL_SCOPE,
'client_secret': config.EMAIL_CLIENT_SECRET,
'grant_type': config.EMAIL_GRANT_TYPE,
'password': config.EMAIL_PASSWORD,
});
let axiosConfig = {
method: 'post',
maxBodyLength: Infinity,
url: `https://login.microsoftonline.com/${config.EMAIL_TENANT_ID}/oauth2/v2.0/token`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
data: data
};
try {
const response = await axios.request(axiosConfig);
return response.data.access_token;
} catch (error) {
console.error("❌ Error getting access token:", error.response?.data || error.message);
throw new Error(`Error getting access token: ${error}`);
}
};
我可以得到令牌。这里没有问题。但是,当我要求使用此令牌发送邮件时,我会遇到此错误。
"error": {
"code": "500",
"message": "EAUTH",
"target": "An error has occurred: Invalid login: 535 5.7.3 Authentication unsuccessful [VI1PR04CA0115.eurprd04.prod.outlook.com 2025-02-20T17:01:08.809Z 08DD4FC050A77D92]", "@Common.numericSeverity": 4
}}
我们与公司的IT团队合作,购买代币时的范围似乎都是正确的。哪里可能会有一个错误。我无法与oauth一起获得验证。
使用Microsoft Graph API发送邮件使用
client_credentials
流,您需要授予应用程序类型API许可到Microsoft Entra ID应用程序。
GRANT
ADMIN同意Mail.Send
申请类型API许可:
对于样品i生成access令牌通过参数以下:
Mail.Send
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id: ClientID
client_secret: Secret
scope: https://graph.microsoft.com/.default
grant_type: client_credentials
许可:
您可以使用Microsoft Graph查询来利用访问令牌来发送邮件:
Mail.Send
如果请求成功,您将获得
错误“访问被拒绝。检查凭据并重试。”
通常发生,如果访问令牌不需要执行操作的权限。因此,请确保授予Microsoft Entra ID应用程序的应用程序类型API权限。
参考:
用户:sendmail -Microsoft Graph V1.0 | Microsoft