如何通过JavaScript SDK发送Outlook邮件?
问题描述:
我搜索了很长时间,但没有找到相关解决方案。如何通过JavaScript SDK发送Outlook邮件?
当我通过微软图形API发送邮件得到403
,我不知道问题出在哪里
{
statusCode: 403,
code: "ErrorAccessDenied",
message: "Access is denied. Check credentials and try again."
}
代码:
makeSilentTokenRequest
//The static key authentication request
function makeSilentTokenRequest(callback) {
// Build up a hidden iframe
var iframe = $('<iframe/>');
iframe.attr('id', 'auth-iframe');
iframe.attr('name', 'auth-iframe');
iframe.appendTo('body');
iframe.hide();
iframe.load(function() {
callback(localStorage.accessToken);
});
iframe.attr('src', buildAuthUrl() + '&prompt=none&domain_hint=' +
localStorage.userDomainType + '&login_hint=' +
localStorage.userSigninName);
}
getAccessToken
//Verify refresh token function
function getAccessToken(callback) {
var now = new Date().getTime();
var isExpired = now > parseInt(localStorage.tokenExpires);
// Do we have a token already?
if (localStorage.accessToken && !isExpired) {
// Just return what we have
if (callback) {
callback(localStorage.accessToken);
}
} else {
// Attempt to do a hidden iframe request
makeSilentTokenRequest(callback);
}
}
个
getUserEmailAddress
function getUserEmailAddress(callback) {
//I saved it in localStorage
if (localStorage.userEmail) {
return localStorage.userEmail;
} else {
//getAccessToken
getAccessToken(function (accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
// Get the Graph /Me endpoint to get user email address
client
.api('/me')
.get((err, res) => {
if (err) {
callback(null, err);
} else {
callback(res.mail);
}
});
} else {
var error = {
responseText: 'Could not retrieve access token'
};
callback(null, error);
}
});
}
}
Sendmail的
//sendMail
function sendMail(message) {
getUserEmailAddress(function (userEmail, error) {
if (error) {
renderError('getUserEmailAddress failed', error.responseText);
} else {
getAccessToken(function (accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
client
.api('/me/sendMail')
.header('Authorization', 'Bearer ' + accessToken)
.header('Content-Type', "application/json")
.post(message, (err, res) => {
if (err) {
console.log(err);
//callback(null, err);
} else {
console.log(res);
//callback(res.value);
}
});
} else {
var error = {
responseText: 'Could not retrieve access token'
};
//callback(null, error);
}
});
}
})
}
createMail
function createMail(message,callback){
getUserEmailAddress(function(userEmail, error) {
if (error) {
renderError('getUserEmailAddress failed', error.responseText);
} else {
getAccessToken(function(accessToken) {
if (accessToken) {
// Create a Graph client
var client = MicrosoftGraph.Client.init({
authProvider: (done) => {
// Just return the token
done(null, accessToken);
}
});
client
.api('/me/messages')
.header('Authorization', 'Bearer ' +accessToken)
.header('Content-Type', "application/json")
.post(message,(err, res) => {
if (err) {
callback(res);
console.log("创建失败!");
} else {
callback(res);
console.log("创建成功!");
}
});
} else {
var error = { responseText: 'Could not retrieve access token' };
//callback(null, error);
}
});
}
})
}
答
没有足够的在这里继续下去,但错误信息是告诉你,你的accessToken
无效。
我看不到您要求哪个范围或您正在执行哪个授权工作流程,所以我无法精确确定它为何无效。
在此先感谢,我将使用方法放入问题中。我使用Microsoft的方式访问'accessToken',并且我可以正常地获取邮件列表,但发送邮件时出现问题。我需要注意哪些特殊问题? – ronger
所以我们有很多获得'accessToken'的微软方法。有多个端点(v1.0 vs v2.0)和多个OAUTH流('authorization_code','implicit','client_credentials'),更不用提多个具有自己范围集的API(Microsoft Graph API,Azure Graph API, Exchange Web服务,Yammer API等)。准确理解你如何获得令牌会告诉我为什么它在这种情况下被拒绝。 –
谢谢,我不认为我说清楚了,我正在使用[Microsoft Graph API](https://developer.microsoft.com/en-us/graph/docs/api-reference/v1.0/api/user_sendmail) – ronger