Javascript 请求\在O365中添加请求

Javascript 请求\在O365中添加请求,javascript,office365api,Javascript,Office365api,我正试图在O365的帮助下发送电子邮件。我已经配置了所有内容,但我得到了一个错误 图示错误{状态代码:405,代码:'Request_BadRequest', 消息:“请求不允许使用指定的HTTP方法。” 目标',请求ID:'34f321-57de-4483-b97d-5957f8786ecb',日期: 2019-09-16T00:17:53.000Z,机身: “{”code:“Request_BadRequest”,“message:“指定的HTTP方法不可用。” 允许请求 目标“,“内部错误

我正试图在O365的帮助下发送电子邮件。我已经配置了所有内容,但我得到了一个错误

图示错误{状态代码:405,代码:'Request_BadRequest',
消息:“请求不允许使用指定的HTTP方法。” 目标',请求ID:'34f321-57de-4483-b97d-5957f8786ecb',日期: 2019-09-16T00:17:53.000Z,机身: “{”code:“Request_BadRequest”,“message:“指定的HTTP方法不可用。” 允许请求 目标“,“内部错误”:{“请求id”:“34f803b1-57de-4483-b97d-5957f8786ecb”,“日期”:“2019-09-16T05:47:51”}” }

代码

const APP_ID = "XXXXXXXXXXXXXXXXXX";
const APP_SECERET = "XXXXXXXXXXXXXX";
const TENANT_ID = "XXXXXXXXXXXXXXXX";
const TOKEN_ENDPOINT = "https://login.microsoftonline.com/XXXXXXXXXXXXXXXXXXXXX/oauth2/v2.0/token";
const MS_GRAPH_SCOPE = "https://graph.microsoft.com/.default";
const GRANT_TYPE = "client_credentials";
const graphScopes = ["User.Read", "Mail.Send"]; // An array of graph scopes

const request = require("request");
const endpoint = TOKEN_ENDPOINT;
const requestParams = {
    grant_type: GRANT_TYPE,
    client_id: APP_ID,
    client_secret: APP_SECERET,
    scope: MS_GRAPH_SCOPE
};
request.post({
    url: endpoint,
    form: requestParams
}, function(err, response, body) {
    if (err) {
        console.log("error");
    } else {
        // console.log(response);
        // console.log("Body=" + body);
        let parsedBody = JSON.parse(body);
        if (parsedBody.error_description) {
            console.log("Error=" + parsedBody.error_description);
        } else {
            console.log("Access Token=" + parsedBody.access_token);
            // testGraphAPI(parsedBody.access_token);
            let accessToken = parsedBody.access_token;
            getMe(accessToken);
        }
    }
});

function getMe(accessToken) {
    require("isomorphic-fetch");
    const fs = require("fs");
    const MicrosoftGraph = require("@microsoft/microsoft-graph-client").Client;
    const options = {
        defaultVersion: "v1.0",
        debugLogging: true,
        authProvider: (done) => {
            done(null, accessToken);
        },
    };

    // https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/samples/node/main.js
    // https://docs.microsoft.com/en-us/graph/overview

    const client = MicrosoftGraph.init(options);

    // send an email
    const sendMail = {
        message: {
            subject: "Test o365 api from node",
            body: {
                contentType: "Text",
                content: "Testing api."
            },
            toRecipients: [{
                emailAddress: {
                    address: "test@abc.com"
                }
            }],
            ccRecipients: [{
                emailAddress: {
                    address: "test@abc.com"
                }
            }]
        },
        saveToSentItems: "false"
    };

    client.api('/users/test1@abc.onmicrosoft.com ').post(sendMail).then((res) => {
        console.log(res);
    }).catch((err) => {
        console.log(err);
    });
}

谁能告诉我哪里出了问题。请帮我解决

我也有同样的错误,发送给用户时url格式不正确

我认为你的url应该是
/users/test1@abc.onmicrosoft.com/sendMail