Forum Discussion
Getting a 401 when trying to create a conversation
Hello !
Using the node botbuilder SDK, I am trying to setup my bot to proactively create a conversation in a public channel on Teams. However, I keep getting 401 responses from the API, with no further details. You can find my code attached bellow:
const adapter = new CloudAdapter(
new ConfigurationBotFrameworkAuthentication(
{
MicrosoftAppId: process.env.MS_BOT_APP_ID,
MicrosoftAppTenantId: process.env.MS_CLIENT_ID,
},
new ConfigurationServiceClientCredentialFactory({
MicrosoftAppId: process.env.MS_BOT_APP_ID,
MicrosoftAppPassword: process.env.MS_BOT_CLIENT_SECRET,
MicrosoftAppTenantId: process.env.MS_CLIENT_ID,
}),
),
);
await adapter.createConversationAsync(
process.env.MS_BOT_APP_ID!,
Channels.Msteams,
"https://smba.trafficmanager.net/teams/",
null,
{
activity: {
type: "message",
text: "This is a test message",
textFormat: "markdown",
},
bot: { id: `28:${process.env.MS_BOT_APP_ID}`, name: "Zygon" },
isGroup: true,
tenantId: process.env.MS_TENANT_ID,
channelData: { channel: { id: channelId } },
},
console.log,
);
8 Replies
- Nivedipa-MSFT
Microsoft
@Euregan - If you're trying to send a proactive message to a Microsoft Teams channel using the Node.js Bot Framework SDK and you're getting a 401 Unauthorized error, it usually means there's a problem with authentication or permissions. Here's a simple checklist to help you troubleshoot:
1.Make sure your bot’s Azure AD app registration has the right Microsoft Graph API permissions. You’ll need:- ChannelMessage.Send
- Chat.ReadWrite
- Team.ReadBasic.All
- Group.Read.All
These should be application permissions (not delegated), and you must grant admin consent after adding them.
2.In your code, you're using:
MicrosoftAppTenantId: process.env.MS_CLIENT_ID,
This is likely incorrect. The MicrosoftAppTenantId should be your Azure AD tenant ID, not the client ID.
Please let us know if you are still facing this issue after troubleshooting.
- Nivedipa-MSFT
Microsoft
@Euregan - If the above solution was helpful, could you please share your valuable feedback via the Microsoft Teams Developer Community Response Feedback link? Your input helps us improve our support.
- EureganCopper Contributor
Thanks for the swift reply!
I have changed the MicrosoftAppTenantId to use process.env.MS_TENANT_ID instead, and I have added three of the four permissions (Chat.ReadWrite, Team.ReadBasic.All, Group.Read.All) as application permissions. However, I can not add ChannelMessage.Send, as it is only available in delegated permissions.
This has not solved my issue, as I still get the same 401 response, "Authorization has been denied for this request." - EureganCopper Contributor
Thanks for the swift reply!
I have changed the MicrosoftAppTenantId to use process.env.MS_TENANT_ID instead, and I have added three of the four permissions (Chat.ReadWrite, Team.ReadBasic.All, Group.Read.All) as application permissions. However, I can not add ChannelMessage.Send, as it is only available in delegated permissions.
This has not solved my issue, as I still get the same 401 response, "Authorization has been denied for this request."- Nivedipa-MSFT
Microsoft
@Euregan - You can't create a new conversation in a public Teams channel using only an application token.
If you need to send messages to a public channel, follow these steps:
- Use a delegated token (via user sign-in) with ChannelMessage.Send.
- Ensure the bot is installed in the team, and capture a conversation reference when it’s first mentioned or added.
- Use that reference with continueConversationAsync() to send messages later.