我能够获取访问 token ,但不确定如何发送消息,因为它需要用户,并且我的应用程序是后端应用程序(nodejs 脚本)。在图形浏览器上,它可以工作。
图形浏览器上的代码片段是:
const options = {
authProvider, //I need this value
};
const client = Client.init(options);
const chatMessage = {body: {content: '@.@@@@.@'}};
await client.api('/teams/my-team-id/channels/my-channel-id/messages')
.post(chatMessage);
如何在nodejs中获取authProvider? 我尝试使用 MSALAuthenticationProviderOptions 但按照以下步骤似乎存在问题(如其 github 存储库中所述):https://www.npmjs.com/package/@microsoft/microsoft-graph-client .
最佳答案
您需要在应用程序而不是用户的上下文中运行它。 Microsoft Graph JavaScript 库现在支持 Azure TokenCredential 来获取 token 。
const { Client } = require("@microsoft/microsoft-graph-client");
const { TokenCredentialAuthenticationProvider } = require("@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials");
const { ClientSecretCredential } = require("@azure/identity");
const { clientId, clientSecret, scopes, tenantId } = require("./secrets"); // Manage your secret better than this please.
require("isomorphic-fetch");
async function runExample() {
const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: [scopes] });
const client = Client.initWithMiddleware({
debugLogging: true,
authProvider,
});
const chatMessage = {body: {content: '@.@@@@.@'}};
const res = await client.api('/teams/my-team-id/channels/my-channel-id/messages')
.post(chatMessage);
console.log(res);
}
runExample().catch((err) => {
console.log("Encountered an error:\n\n", err);
});
该样本来自:
关于javascript - 使用nodejs向微软团队 channel 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67909868/