授权拒绝尝试访问 Microsoft Graph 中的预订 Api

Authorization Denied trying to access Bookings Api in Microsoft Graph

我正在尝试创建一个从 AWS lambda 运行的应用程序,它充当希望使用 Microsoft Bookings 注册预订的客户的中间人。按照文档,我能够为 Graph 生成访问令牌,但是当我尝试通过 api.

请求预订信息时,我得到了授权拒绝

我的代码:

import request from "request";
import { Callback } from "./callback";

const APP_ID = process.env.BOOKINGS_APP_ID;
const APP_SECRET = process.env.BOOKINGS_API_SECRET;
const TOKEN_ENDPOINT = `https://login.microsoftonline.com/${process.env.BOOKINGS_TENANT_NAME}.onmicrosoft.com/oauth2/token`;

const requestParams = {
    client_id: APP_ID,
    client_secret: APP_SECRET,
    grant_type: "client_credentials",
    resource: "https://graph.microsoft.com",
    scope: "https://graph.microsoft.com/.default",
};

export default class Bookings {
    public static async listBusinesses(callback: Callback) {
        Bookings.generateAPIToken((err: string | null, data: any) => {
            const options = {
                // body: {},
                headers: {
                    "Authorization": `Bearer ${data}`,
                    "Content-Type": "application/json",
                },
                method: "GET",
                url: "https://graph.microsoft.com/beta/bookingBusinesses",
            };

            console.log(data);

            return request(options, (error: string, res: any) => {
                if (error) {
                    return callback(error, {});
                }

                return callback(null, JSON.parse(res.body));
            });
        });
    }

    public static async generateAPIToken(callback: Callback) {
        request.post({ url: TOKEN_ENDPOINT, form: requestParams }, (err, res, body) => {
            if (err) {
                return callback(err, {});
            }

            return callback(null, JSON.parse(body).access_token);
        });
    }
}

我得到的错误:

{
    error: {
        code: '',
        message: 'Authorization has been denied for this request.',
        innerError: {
            'request-id': '695d3f90-357d-490c-b980-4a2018dd39a5',
            date: '2020-06-08T03:21:59'
        }
    }
}

我也尝试过使用 microsoft-graph-client 库来访问预订,但这也不起作用。我做错了什么?感谢您的帮助。

我们可以看到document显示了您要请求的图表api(bookingBusinesses)需要委托类型权限并且不支持应用程序类型权限。

所以我们不能使用 "client_credentials" 授权流程,您的代码显示您使用 "client_credentials" 作为授权类型。您可以使用 "username/password" grant flow 来获取访问令牌。因此,您请求访问令牌的参数应如下所示:

const requestParams = {
    client_id: APP_ID,
    client_secret: APP_SECRET,
    grant_type: "password",
    scope: "https://graph.microsoft.com/.default",
    username: "your user name/email(like xxxxx@xxx.onmicrosoft.com)",
    password: "your password"
};

顺便说一下,我注意到你代码中的 "TOKEN_ENDPOINT" 是 https://login.microsoftonline.com/${process.env.BOOKINGS_TENANT_NAME}.onmicrosoft.com/oauth2/token 并且你在 requestParams 中同时使用了参数 resourcescope。如果我们使用 v1 端点作为您的代码,我们只需要使用参数 resource。如果我们使用 v2 端点(https://login.microsoftonline.com/${process.env.BOOKINGS_TENANT_NAME}.onmicrosoft.com/oauth2/v2.0/token),我们需要使用参数 scope 而不是参数 resource。我上面提供的代码使用 v2,所以我使用 scope 参数,您还需要将 "TOKEN_ENDPOINT" 更改为 v2(只需在 oauth2/ 和 [= 之间添加一个 v2.0 23=]).

如果您不想将 "TOKEN_ENDPOINT" 更改为 v2,只需使用如下参数:

const requestParams = {
    client_id: APP_ID,
    client_secret: APP_SECRET,
    grant_type: "password",
    resource: "https://graph.microsoft.com",
    username: "your user name/email(like xxxxx@xxx.onmicrosoft.com)",
    password: "your password"
};

希望对你有帮助~