简体   繁体   English

如何使用Ajax调用获取Microsoft Graph API Access令牌

[英]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.

相关问题 如何从 Azure Active Directory 获取我的 API 和 Microsoft Graph 的有效访问令牌? - How to Get a valid access token for my API and Microsoft Graph from Azure Active Directory? 如何获取USER访问令牌,与Graph API Explorer中使用的令牌相同 - How to get USER access token, the same used in Graph API Explorer 使用Facebook图形API获取页面访问令牌 - Get a page access token with Facebook graph api 如何使用 Microsoft Graph API 获取用户的照片(个人资料)? - How to get user's photo(profile) using Microsoft Graph API? 如何使用 Microsoft graph API 获取响应标头 - How to get a response headers using Microsoft graph API Facebook图形API调用以获取应用程序访问令牌返回“未知错误” - Facebook graph api call to get app access token returns “unknown error” Facebook Open Graph API调用问题...需要一个有效的访问令牌吗? - Problem with Facebook Open Graph API call… need an active access token? 如何使用 nodejs 和 node-fetch 从 Azure 为 Microsoft Graph 获取客户端令牌 - How to get a client token from Azure for Microsoft Graph using nodejs and node-fetch 如何在 javascript cookie 中存储访问令牌值并使用 AJAX 调用将该令牌传递给 Header - How to store Access token value in javascript cookie and pass that token to Header using AJAX call 如何从 microsoft graph 获取刷新令牌 - How do I get refresh token from microsoft graph
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM