IT虾米网

javascript之O365 中的 Request_BadRequest

lautakyan007 2024年02月06日 程序员 26 0

我正在尝试在 O365 的帮助下发送电子邮件。我已经配置了所有内容,但出现错误

GraphError { statusCode: 405, code: 'Request_BadRequest',
message: 'Specified HTTP method is not allowed for the request target.', requestId: '34f321-57de-4483-b97d-5957f8786ecb', date: 2019-09-16T00:17:53.000Z, body: '{"code":"Request_BadRequest","message":"Specified HTTP method is not allowed for the request target.","innerError":{"request-id":"34f803b1-57de-4483-b97d-5957f8786ecb","date":"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://learn.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); 
    }); 
} 

有人可以告诉我哪里出了问题吗?请帮帮我

请您参考如下方法:

我遇到了同样的错误,发送给用户时这是一个格式错误的网址。

我认为您的网址应该是 /users/test1@abc.onmicrosoft.com/sendMail


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!