[英]How to Get a valid access token for my API and Microsoft Graph from Azure Active Directory?
[英]How to get Microsoft Graph API Access token using ajax call
我在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>
感謝您的解決方案。 我現在能夠得到它。 您能否讓我知道我是否可以使用相同的承載令牌獲得Outlook任務。 我在URL下面嘗試了同樣的方法。
https://graph.microsoft.com/beta/me/outlook/tasks
但是獲取訪問被拒絕錯誤。
我也試過
https://graph.microsoft.com/v1.0/users/userid/outlook/tasks
但顯示“錯誤請求”
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.