Search code examples
azureazure-active-directory

Microsoft Graph API - Sending message in a channel error


Get error response when calling the below API. I think I have all the permissions needed for this API. Any idea what I am missing here? Thanks!

Request: POST /teams/{team-id}/channels/{channel-id}/messages Authorization: Bearer <access_token>

Response: 401 { "error": { "code": "Unauthorized", "message": "Message POST is allowed in application-only context only for import purposes. Refer to https://docs.microsoft.com/microsoftteams/platform/graph-api/import-messages/import-external-messages-to-teams for more details." } }

Checking all the required permissions needed for this API.


Solution

  • The error usually occurs if you are using client credentials flow to generate bearer token where Application permissions only work with API calls.

    I tried to reproduce the same in my environment and got below results:

    I registered one Azure AD application and added all required API permissions like below:

    enter image description here

    Now, I generated one bearer token using client credentials flow via Postman with below parameters:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:client_credentials
    client_id: <appID>
    client_secret: <secret> 
    scope: https://graph.microsoft.com/.default
    

    Response:

    enter image description here

    When I used this token to send message by calling below query, I got same error like this:

    POST https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages
    {
      "body": {
        "content": "Hello World"
      }
    }
    

    Response:

    enter image description here

    To resolve the error, you need to use Delegated flows like Authorization code flow to generate bearer token.

    You can make use of below authorization request to get code value:

    https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/authorize
    ?client_id=<appID>
    &response_type=code
    &redirect_uri=https://jwt.ms
    &response_mode=query
    &scope= https://graph.microsoft.com/.default
    &state=12345
    

    When I ran above request in browser, I got code value in address bar after signing in successfully like below:

    enter image description here

    Now, I generated access token using authorization code flow via Postman with below parameters like this:

    POST https://login.microsoftonline.com/<tenantID>/oauth2/v2.0/token
    grant_type:authorization_code
    client_id: <appID>
    client_secret: <secret>
    scope: https://graph.microsoft.com/.default
    code: <paste_code_from_above_request>
    redirect_uri: https://jwt.ms
    

    Response:

    enter image description here

    When I used this token to send message in channel by calling below query, I got response successfully like below:

    POST https://graph.microsoft.com/v1.0/teams/{team-id}/channels/{channel-id}/messages
    {
      "body": {
        "content": "Hello World"
      }
    }
    

    Response:

    enter image description here