Ajax 为 Azure Ad Graph API 获取请求 returns "Unauthorized"

Ajax get Request returns "Unauthorized" for Azure Ad Graph API

我正在尝试从我的 typescript 代码中调用 Azure Ad Graph API,但我一直收到错误消息:Unauthorized

lavaNET.SharePointREST.getJsonWithoutSite(this, "https://graph.windows.net/lavanet.dk/users?api-version=1.6", (tmplData: any, tmplTextStatus: string, tmplXHR: JQueryXHR) => {
    console.log(tmplData)
});

export function getJsonWithoutSite(context: any, url: string, success: SuccessCallback, failure?: ErrorCallback) {

    $.ajax({
        method: 'GET',
        context: context,
        headers: {
            'Authorization': 'Bearer zc21eb27-760b-4b46-828e-xxxxxxxxxxxxx',
            'Content-Type': 'application/x-www-form-urlencoded'
        },
        url: url,
        dataType: 'json',
        accepts: { json: 'application/json; odata=verbose' },
        success: success,
        error: failure || function (jqXHR, textStatus, errorThrown) { alert('Error: ' + errorThrown); }
    });
}

有人可以指出为什么我无法从广告中获取数据吗?

更新 这是我需要的还是我完全错了:

更新 作为对 Gary Liu - MSFT 的回复,我从他的代码中获得了这些密钥:

我的 Ajax 正确吗?我应该使用哪个密钥?

您已生成访问令牌,并将其设置在 Authorization header。

我们需要手动按照 OAuth 2.0 authorization flow 来实现在我们的应用程序中从 Azure AD 获取访问令牌的功能。

您还可以利用 ADAL SDK 在您的应用中轻松实现此功能。但是,目前不存在 ts 的此类 ADAL。您可以尝试利用 ADAL for js 来扩展访问令牌,将其存储在客户端浏览器的 session 存储或本地存储中。并在您的 ts 脚本中使用令牌。

这里 sample 介绍了如何在普通 javascript 中为 js 使用 ADAL。