使用ajax调用获取Microsoft Graph API访问令牌或在iFrame中使用Microsoft Graph API不起作用 [英] Get Microsoft Graph API Access token using ajax call or use of Microsoft Graph API in iFrame does not working
问题描述
您好,
我在SharePoint Online页面上使用Microsoft Graph API从Outlook日历中获取用户的事件。我正在使用ADAL.JS.当我转到该页面时,页面重定向到MS登录以从Azure AD获取访问令牌并再次进入页面。
我尝试使用ajax调用获取访问令牌,但是令牌不起作用。我试图在另一个页面上的iFrame中调用该页面,但它没有在iFrame中工作。
你能否建议我是否可以在后台获取访问令牌以便页面可以没有重定向到Microsoft登录。
谢谢,
嗨Rajesh,
您需要在此网址中向SharePoint Online租户帐户注册一个应用程序:
谢谢
Hello,
I am using Microsoft Graph API on a SharePoint Online page to get user's events from outlook calendar. I am using 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.
I tried to get access token using ajax call, but token does not working. I tried to call that page in iFrame on another page, but it is not getting work in iFrame.
Can you anyone suggest if I can get access token in background so that page does not redirected to Microsoft login.
Thanks,
Hi Rajesh,
You will need to register an application with your SharePoint Online Tenant account from this url:
https://apps.dev.microsoft.com
Then use the script below with App Id and secret which got from the application above:
<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 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; } }) } </script> <p id="content"></p>
Reference here:
How To Fetch Access Token Using Microsoft Graph API
Thanks
这篇关于使用ajax调用获取Microsoft Graph API访问令牌或在iFrame中使用Microsoft Graph API不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!