JavaScript的Microsoft Graph示例代码段不起作用



我一直在学习这个库的教程,但提供的代码片段会产生错误。我已经在Azure注册了该应用程序,并遵循了说明,但当我运行代码时,它显示SyntaxError: await is only valid in async functions and the top level bodies of modules at /script.js:74:20

这里有一段相关的代码片段,但如果您有Replit,如果您能与我合作编写Repl,我将不胜感激

回复链接:https://replit.com/join/rgqcqfcohh-5pengoo

代码:

const msal = require('@azure/msal-node');
// Create msal application object
const cca = new msal.ConfidentialClientApplication(config);
const REDIRECT_URI = "http://localhost:3000/redirect";
const config = {
auth: {
clientId: "ebcb2e8c-4675-411f-a76e-25aafe0c026d",
authority: "https://login.microsoftonline.com/98ca2106-858a-413a-b7d5-31301dcf9869/",
// I wasn't sure if this meant the key value or the secret ID
clientSecret: "ee10b5ce-f9c4-460a-a402-064030841f86"
},
system: {
loggerOptions: {
loggerCallback(loglevel, message, containsPii) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: msal.LogLevel.Verbose,
}
}
};
// 1st leg of auth code flow: acquire a code
app.get('/', (req, res) => {
const authCodeUrlParameters = {
scopes: ["user.read"],
redirectUri: REDIRECT_URI,
};
// get url to sign user in and consent to scopes needed for application
pca.getAuthCodeUrl(authCodeUrlParameters).then((response) => {
res.redirect(response);
}).catch((error) => console.log(JSON.stringify(error)));
});
// 2nd leg of auth code flow: exchange code for token
app.get('/redirect', (req, res) => {
const tokenRequest = {
code: req.query.code,
scopes: ["user.read"],
redirectUri: REDIRECT_URI,
};
pca.acquireTokenByCode(tokenRequest).then((response) => {
console.log("nResponse: n:", response);
res.sendStatus(200);
}).catch((error) => {
console.log(error);
res.status(500).send(error);
});
});
try {
let userDetails = await client.api("/me").get();
console.log(userDetails);
} catch (error) {
throw error;
}

MohammedMehtabSiddiqueMINDTREELIMI-9821在微软文档上告诉我。。。

"您可以使用";等待";仅在一个函数内部;异步";。

在这里,您可以尝试从代码中删除"wait"并尝试运行它;

它成功了!

相关内容

最新更新