[英]How to get Microsoft Graph API Access token using ajax call
I am using Microsoft Graph API on a SharePoint Online page to get user's events from outlook calendar. 我在SharePoint Online页面上使用Microsoft Graph API从Outlook日历获取用户事件。 I am using ADAL.JS. 我正在使用ADAL.JS。 When I go to that page, the page redirected to MS login to get access token from Azure AD and come to page again. 当我转到该页面时,该页面将重定向到MS登录以从Azure AD获取访问令牌,然后再次进入该页面。
I tried to get access token using ajax call, but token does not working. 我尝试使用ajax调用获取访问令牌,但是令牌不起作用。 I tried to call that page in iFrame on another page, but it is not getting work in iFrame. 我试图在另一个页面上的iFrame中调用该页面,但是在iFrame中无法正常工作。
Can you anyone suggest if I can get access token in background so that page does not redirected to Microsoft login. 有人可以建议我是否可以在后台获取访问令牌,以便该页面不会重定向到Microsoft登录名。
We tried below code, but it is giving error as "No mailbox was found that includes the specified identity: xxxxxxx" 我们尝试了以下代码,但由于“找不到包含指定标识的邮箱: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>
Thanks, 谢谢,
When I test this in my online environment follow this thread , the request fail as it require user consent( official guideline ). 当我在我的在线环境中按照此线程对其进行测试时 ,该请求失败,因为它需要用户的同意( 官方指南 )。
So I grant the app Application Permissions and approve it by admin with admin consent url( https://login.microsoftonline.com/common/adminconsent?client_id=appid&state=12345 ) 因此,我授予了该应用程序“应用程序权限”,并由管理员使用管理员同意的URL进行批准( https://login.microsoftonline.com/common/adminconsent?client_id=appid&state=12345 )
Now, I could access the calendar view by below endpoint: 现在,我可以通过以下端点访问日历视图:
https://graph.microsoft.com/v1.0/users/userid/calendarView/delta?startdatetime=2018-12-04T12:11:08Z&enddatetime=2019-01-04T12:11:08Z
My test code: 我的测试代码:
<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>
Thanks for the solution. 感谢您的解决方案。 I am able to get it now. 我现在能够得到它。 Can you please let me know if I can get outlook tasks using same bearer token. 您能否让我知道我是否可以使用相同的承载令牌获得Outlook任务。 I tried same by below URL. 我在URL下面尝试了同样的方法。
https://graph.microsoft.com/beta/me/outlook/tasks https://graph.microsoft.com/beta/me/outlook/tasks
But getting access denied error. 但是获取访问被拒绝错误。
I also tried 我也试过
https://graph.microsoft.com/v1.0/users/userid/outlook/tasks https://graph.microsoft.com/v1.0/users/userid/outlook/tasks
but it says "Bad request" 但显示“错误请求”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.