【问题标题】:Does anyone have a working example of how to call Microsoft Graph from Angular using ADAL?有没有人有一个如何使用 ADAL 从 Angular 调用 Microsoft Graph 的工作示例?
【发布时间】:2018-09-20 09:07:05
【问题描述】:

我已经浏览了所有可以使用 ADAL.js(不是 msal)从最近的角度版本调用 Microsoft Graph 的示例,但到目前为止还没有运气。那么有什么有效的例子吗?我发现的最近的示例都是针对 msal - 只要 azure 广告应用程序无法升级到 v2 端点,这无关紧要......

【问题讨论】:

    标签: angular microsoft-graph-api single-page-application adal adal.js


    【解决方案1】:

    仅供参考:

    https://gist.github.com/psignoret/50e88652ae5cb6cc157c09857e3ba87f

    <script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.11/js/adal.min.js"></script>
    <script type="text/javascript">
                // Set up ADAL
                var authContext = new AuthenticationContext({
                    clientId: '057e09f6-5d1a-43f9-abf5-451f20ab177f',
                    postLogoutRedirectUri: 'http://bl.ocks.org/psignoret/raw/50e88652ae5cb6cc157c09857e3ba87f/'
                });
                // Make an AJAX request to the Microsoft Graph API and print the response as JSON.
                var getCurrentUser = function (access_token) {
                    document.getElementById('api_response').textContent = 'Calling API...';
                    var xhr = new XMLHttpRequest();
                    xhr.open('GET', 'https://graph.microsoft.com/v1.0/me', true);
                    xhr.setRequestHeader('Authorization', 'Bearer ' + access_token);
                    xhr.onreadystatechange = function () {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            // Do something with the response
                            document.getElementById('api_response').textContent =
                                JSON.stringify(JSON.parse(xhr.responseText), null, '  ');
                        } else {
                            // TODO: Do something with the error (or non-200 responses)
                            document.getElementById('api_response').textContent =
                                'ERROR:\n\n' + xhr.responseText;
                        }
                    };
                    xhr.send();
                }
                if (authContext.isCallback(window.location.hash)) {
                    // Handle redirect after token requests
                    authContext.handleWindowCallback();
                    var err = authContext.getLoginError();
                    if (err) {
                        // TODO: Handle errors signing in and getting tokens
                        document.getElementById('api_response').textContent =
                            'ERROR:\n\n' + err;
                    }
                } else {
                    // If logged in, get access token and make an API request
                    var user = authContext.getCachedUser();
                    if (user) {
                        document.getElementById('username').textContent = 'Signed in as: ' + user.userName;
                        document.getElementById('api_response').textContent = 'Getting access token...';
                        
                        // Get an access token to the Microsoft Graph API
                        authContext.acquireToken(
                            'https://graph.microsoft.com',
                            function (error, token) {
                                if (error || !token) {
                                    // TODO: Handle error obtaining access token
                                    document.getElementById('api_response').textContent =
                                        'ERROR:\n\n' + error;
                                    return;
                                }
                                // Use the access token
                                getCurrentUser(token);
                            }
                        );
                    } else {
                        document.getElementById('username').textContent = 'Not signed in.';
                    }
                }
            </script>
    

    这适用于我这边的 Firefox。

    【讨论】:

    • postLogoutRedirectUri: 应该是什么?
    猜你喜欢
    • 1970-01-01
    • 2013-05-16
    • 2011-07-16
    • 2022-11-15
    • 1970-01-01
    • 2010-10-03
    • 2022-11-09
    • 2023-03-22
    • 2012-04-29
    相关资源
    最近更新 更多