javascript - 如何使用 ajax 调用获取 Microsoft Graph API 访问令牌
问题描述
我在 SharePoint Online 页面上使用 Microsoft Graph API 从 Outlook 日历获取用户事件。我正在使用 ADAL.JS。当我转到该页面时,该页面重定向到 MS 登录以从 Azure AD 获取访问令牌并再次进入页面。
我尝试使用 ajax 调用获取访问令牌,但令牌不起作用。我试图在另一个页面上的 iFrame 中调用该页面,但它在 iFrame 中没有工作。
谁能建议我是否可以在后台获取访问令牌,以便该页面不会重定向到 Microsoft 登录。
我们尝试了下面的代码,但它给出的错误是“找不到包含指定身份的邮箱:xxxxxxx”
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/tenantname.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of tenantname
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id": "****************************", //Provide your app id
"client_secret": "******************", //Provide your client secret
"scope": "https://graph.microsoft.com/.default"
},
success: function(response) {
console.log(response);
token = response.access_token;
document.getElementById('content').innerHTML = token;
}
})
}
</script>
<p id="content"></p>
谢谢,
解决方案
当我在我的在线环境中按照这个线程进行测试时,请求失败,因为它需要用户同意(官方指南)。
因此,我授予应用程序应用程序权限并由管理员使用管理员同意 url 批准(https://login.microsoftonline.com/common/adminconsent?client_id=appid&state=12345)
现在,我可以通过以下端点访问日历视图:
https://graph.microsoft.com/v1.0/users/userid/calendarView/delta?startdatetime=2018-12-04T12:11:08Z&enddatetime=2019-01-04T12:11:08Z
我的测试代码:
<script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script type="text/javascript">
$(document).ready(function () {
requestToken();
});
var token;
function requestToken() {
$.ajax({
"async": true,
"crossDomain": true,
"url": "https://cors-anywhere.herokuapp.com/https://login.microsoftonline.com/tenant.onmicrosoft.com/oauth2/v2.0/token", // Pass your tenant name instead of sharepointtechie
"method": "POST",
"headers": {
"content-type": "application/x-www-form-urlencoded"
},
"data": {
"grant_type": "client_credentials",
"client_id ": "xxx", //Provide your app id
"client_secret": "xxx", //Provide your client secret genereated from your app
"scope ": "https://graph.microsoft.com/.default"
},
success: function (response) {
console.log(response);
token = response.access_token;
document.getElementById('content').innerHTML = token;
$.ajax({
url: 'https://graph.microsoft.com/v1.0/users/userid/calendarView/delta?startdatetime=2018-12-04T12:11:08Z&enddatetime=2019-01-04T12:11:08Z',
type: 'GET',
dataType: 'json',
beforeSend: function (xhr) {
xhr.setRequestHeader('Authorization', 'Bearer '+token+'');
},
data: {},
success: function (results) {
console.log(response);
debugger;
},
error: function (error) {
console.log("Error in getting data: " + error);
}
});
}
})
}
</script>
推荐阅读
- python - 从数据框中删除异常值和周围数据
- c# - app.config 中的修改在执行项目时没有反映
- machine-learning - 使用 LSTM Keras 的序列数据进行多类分类不起作用
- python - 从其他 python 脚本更改 PyQt 的 QLabel
- java - 因“方法代码太大”而引发 GSP 异常?
- markdown - 通过 Pandoc 将 Markdown 格式的 Agda 读写到 LaTeX
- perl - 如何从perl中的给定json文件中提取键和值
- amazon-web-services - 如何使用 AWS 资源安排“aws s3 sync s3://bucket1 s3://bucket2”?
- json - Wcf 请求总是带有空参数
- java - 通过传递变量动态打开 FXML 接口