我正在加载项中工作,这就是我使用 Office 对话框登录的原因
var url = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=myclientid&response_type=token&redirect_uri=https://localhost:3000/assets/Redirect.html&scope=user.read%20mail.readwrite%20mail.send&response_mode=fragment&state=12345&nonce=678910";
Office.context.ui.displayDialogAsync(url, { height: 60, width: 40 }, function (asyncResult) {
Logindialog = asyncResult.value;
Logindialog.addEventHandler(Office.EventType.DialogMessageReceived, function (arg) {
token = arg.message;
});
});
在我的
redirect.html
文件中使用它成功登录后,我获得了令牌
var address_Link = window.location.href;
console.log(address_Link)
var address_arr = address_Link.split("=");
var address_Token = address_arr[1];
var Id_Token = address_Token.split("&");
var access_token = Id_Token[0];
console.log(Id_Token);
Office.onReady(function (appInfo) {
Office.context.ui.messageParent(JSON.stringify(access_token));
})
我的简单 Microsoft 帐户令牌如下所示
EwCoA8l6BAAUs5%2bHQn0N%2bh2FxWzLS31ZgQVuHsYAASzDAo%2bzOHW8bimiwipl3VtTsk4JI1MUN2Osop4ryl1wGSRhZypgzfO/ff1zkOGAzStT4BsAzkSOV/0LiCtQ2MTB2RsR1%2buhflh 3Pq2CwOawwTS7nj/%2bKnDFvH3N9h6N8uwruiEH4OZekE0gfk4fOfY2Go5IgrWUfbs%2bjcbNXKGv/Ekc4PesSXNaII5mCyBjLVuvxvLHhrZzAL7tu8AxCFyd5XxAWP6pXjJRZk7ow0ChUQeNNPNP5/ rWSTLBbMu3AORmKIi%2bvfdvVcxhmmQmpAgU4glN6BIR7gKj6pkvhKQVydGIvTPvFlLOvWJItF5um8ignPEgP43gMqmPsH07xFJcDZgAACBbA8fXreaJdeAK%2bseTrYHH7KxKhahPIN2sZUfAZKPHjuoPq hQopFvQFxWUljFolJzHWn00pHbW3pVm95S58HngtIYDzNg9uKqIm9O3VJdrwctgnsDA5cMNPFzyOb3nYh58BSdefe3c42D1n7ovKtUjKtQR%2b5zJrocarrwXhYLvXbAg2kMliEc1zqiCbyH6ila 5s63mWaoufy%2bF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1kYmFhYmMwNmMwNGYiLCJ1bmlxdWVfbmFtZSI6ImZ1bmRhdGlvQGZ1bmRhdGlvLm9yZyIsInVwbiI6ImZ1bmRhdGlvQGZ1bmRhdGlvLm9yZyIsInV0 ASI6Imd1YjRCNERVNTBHY1BOeXVsRk1HQVEiLCJ2ZXIiOiIxLjAiLCJ4bXNfc3QiOnsic3ViIjoiVFRJcVNfanhaNXAyRnpROVJPaG5tZWxLVjNrRkFPdmQ5Z2leUZySU15QSJ9LCJ4bXNfdGNkdCI6 MTYxODQ4MjgyNCwieG1zX3RkynIiOiJFVSJ9.LS0n3DWrv-YHufr9oA4PW97_nwQ_oP6Z4e-jdWvQrU97nznZQs9w-qfeRvtijUXvmVEGo8fyUgCCZ1UNLB3pbOIWZYIOPhXmsGiWs5sjdn9-vKT- 394jBUqCqkaLXa2aldBWGUmklDJ81TYzFV4C7BEjQang162KjHqetXg118OEmDZaayd1VyNWIc2xQ6RK-gmxvPOqv8CPIAOj4fQ8HcQxpUtpSONr98mVwX4potYpJVV2Vn6-GALF86DYNrxw1rBt3qOM hCInHMwxgQHsLK_XO2tnSoJ-LZzk5NusOOPnM4GVrcsKgA9jUK3NwBrJYpda2RAkcHfcM5hkOYxc1A
error status of 401
这些令牌之间有什么区别,我如何使用这两个令牌发送邮件?作为解决方法,请使用 Microsoft 域用户,例如
。[email protected]
创建 Microsoft Entra ID 应用程序并授予 API 权限:
现在我使用以下端点生成了auth-code:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/authorize?
&client_id=ClientID
&response_type=code
&redirect_uri=https://jwt.ms
&response_mode=query
&scope=https://graph.microsoft.com/.default
&state=12345
使用以下参数通过 Postman 生成访问令牌:
https://login.microsoftonline.com/TenantID/oauth2/v2.0/token
client_id:ClientID
scope:https://graph.microsoft.com/.default
grant_type:authorization_code
code:code
redirect_uri:https://jwt.ms
client_secret:Secret
解码访问令牌:
现在通过使用上面的访问令牌,我能够成功发送邮件:
POST https://graph.microsoft.com/v1.0/me/sendMail
Content-type: application/json
{
"message": {
"subject": "Meet for lunch?",
"body": {
"contentType": "Text",
"content": "The new cafeteria is open."
},
"toRecipients": [
{
"emailAddress": {
"address": "xxx.onmicrosoft.com"
}
}
],
"ccRecipients": [
{
"emailAddress": {
"address": "xxx.onmicrosoft.com"
}
}
]
},
"saveToSentItems": "false"
}
参考: