javascript - 使用nodejs向微软团队 channel 发送消息

标签 javascript node.js oauth-2.0 microsoft-graph-api

我能够获取访问 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);
});

该样本来自:

https://github.com/microsoftgraph/msgraph-sdk-javascript/tree/dev/samples/tokenCredentialSamples/ClientCredentialFlow

关于javascript - 使用nodejs向微软团队 channel 发送消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67909868/

相关文章:

javascript - 如何将创建的 PDF 文件中的 base64 字符串发送给客户端?

javascript - 将 morgan 日志存储到数据库中

symfony - FOSOAuthServerBundle:将 access_token 嵌入授权 header 中

javascript - 为什么这个 HTML 按钮不起作用?

javascript - 将数组转换为动态对象

javascript - 强制启动选项卡处于事件状态

python - botocore.errorfactory.InvalidS3ObjectException

javascript - 执行Jquery function()的两个条件

ios - 使用 iOS/swift 对 OAuth2 和 Eventbrite 进行身份验证时出现 “Oops, something went wrong” 错误

oauth-2.0 - 谷歌云打印获取访问 token