Azure AD 仅生成无效的访问令牌以用于 Microsoft Graph

Azure AD only generates invalid Access Token to use with Microsoft Graph

我已经在 Azure Active Directory 中设置了应用程序注册,以便我可以通过 Microsoft Graph 访问 Microsoft OneNote notebooks/sections/pages。

它几乎允许每个 Microsoft 持有帐户登录,因此我使用 https://login.microsoftonline.com/common/oauth2/v2.0/authorize 作为我的授权端点,使用 https://login.microsoftonline.com/common/oauth2/v2.0/token 作为我的令牌端点。 在应用程序注册中,我将 API 权限设置为:

我在我的重定向 URI 中添加了一个本地主机,它被列为“web”,这样我就可以跟进流程。

我遇到的问题是,当我最终收到我的 access_token 时,我在将其用作 Bearer Token 时收到错误消息:graph.microsoft.com/v1.0/me/onenote/notebooks

{
    "error": {
        "code": "40001",
        "message": "The request does not contain a valid authentication token. Detailed error information: {0}",
        "innerError": {
            "date": "2020-10-07T20:37:37",
            "request-id": "c8b0c20e-d096-4fcb-9e97-841b1626537c",
            "client-request-id": "a-uuid"
        }
    }
}

所以按照流程,我有这样的东西:

  1. 通过此进行身份验证url

https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=99e1fc5a-bl4h-bl4h-bl4h-l0ng3rbl4h&redirect_uri=http://localhost/myapp&response_type=code&scope=https://graph.microsoft.com/User.Read openid offline_access https://graph.microsoft.com/Notes.Read.All&state=abc&response_mode=query

我有点不确定我在这里使用的范围变量是否正确。我希望能够访问所有用户 OneNote notebooks/sections/pages 并阅读他们的个人资料并离线执行操作,

  1. 当我的用户成功通过身份验证时,代码将重定向到此处:

http:///localhost/myapp?code=M.R3_BL2.15c2b73a-486c-c0f6-95c1-8432603aa7a4&state=abc

  1. 我从查询字符串中提取 code 并制作一个 POST 像:
curl --location --request POST 'https://login.microsoftonline.com/common/oauth2/v2.0/token' \
--form 'code=M.R3_BL2.15c2b73a-486c-c0f6-95c1-8432603aa7a4' \
--form 'grant_type=authorization_code' \
--form 'client_id=99e1fc5a-bl4h-bl4h-bl4h-l0ng3rbl4h' \
--form 'scope=https://graph.microsoft.com/User.Read openid offline_access https://graph.microsoft.com/Notes.Read.All' \
--form 'client_secret=shhhhASecret'

现在这将 return 我 access_token JSON。

{
    "token_type": "Bearer",
    "scope": "https://graph.microsoft.com/User.Read openid https://graph.microsoft.com/Notes.Read.All",
    "expires_in": 3600,
    "ext_expires_in": 3600,
    "access_token": "EwCQA8l6BAAUO9chh8cJscQLmU+longstring",
    "refresh_token": "M.R3_BL2.CecNbvRse*longstring",
    "id_token": "eyJ0eXAiOiJKlongstring"
}

但是,如前所述,此访问令牌似乎无法让我访问 Microsoft Graph。我已尝试在我的请求中使用范围的不同变体,但 none 似乎生成了正确的访问令牌。

我用于从 node.js

调用 Microsoft Graph 的代码
const http = require('https');

class OneNote {
constructor(bearer) {
        this.bearer = bearer;
        this.HTTP_CODES = {
            OK: 200,
            Unauthorized: 401
        };
    }

async getNotebooks() {
        const options = this._getOptions();
        options.path += 'notebooks';
        return this._requestData(options)
            .catch((err) => {
                throw err;
            });
    }

_getOptions() {
        return {
            method: 'GET',
            hostname: 'graph.microsoft.com',
            path: '/v1.0/me/onenote/',
            headers: {
                'Authorization': `Bearer ${this.bearer}`
            }
        }
    }

    async _requestData(options) {
        console.log(options)
        return new Promise((resolve, reject) => {
            const req = http.request(options, (res) => {
                let data = '';
                res.on('data', (d) => {
                    data += d;
                });
                if (res.statusCode === this.HTTP_CODES.Unauthorized) {
                    res.on('end', () => {
                        reject(data);
                    });
                } else {
                    res.on('end', () => {
                        resolve(data);
                    });
                }
            });

            req.on('error', (err) => {
                reject(err);
            });
            req.end();
        });
    }
}

您可能使用了错误的权限(范围)。

基于 Permissions for listing notebooks,个人 Microsoft 帐户所需的权限为 Notes.Create, Notes.Read, Notes.ReadWrite

Notes.Read.All这里不需要。

所以请在范围内添加Notes.Read委托权限来解决这个问题。