Cannot query graph.microsoft.com API

BugBug IT 0 Reputation points
2024-02-16T08:24:14.94+00:00

I'm trying to use the graph.microsoft.com API to retrieve users and channels from Microsoft Teams and send messages to those channels. I'm using Python and trying to accomplish these tasks.

Access token fetch through msal library using oauth 2.0 (initiate_auth_code_flow + acquire_token_by_auth_code_flow)

import requests

url = "https://graph.microsoft.com/v1.0/users"
headers = {
    "Authorization": "Bearer <TOKEN>",
    "Content-Type": "application/json"
}

response = requests.get(url, headers=headers)

print(response.content)

However, I'm encountering different errors depending on the endpoint used:

  1. For /me, I receive a valid response.
   {"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users/$entity","userPrincipalName":"******@bugbug.io","id":"905eed23ad42a35c","displayName":"BugBug IT","surname":"IT","givenName":"BugBug","preferredLanguage":"en-US","mail":"******@bugbug.io","mobilePhone":null,"jobTitle":null,"officeLocation":null,"businessPhones":[]}'
   
  1. For /users, I receive an InternalServerError with the message "Invalid response. Expected list response must have value property."
   {"@odata.context":"https://graph.microsoft.com/v1.0/$metadata#users","value":[{"error":{"code":"InternalServerError","message":"Invalid response. Expected list response must have value property.","innerError":{"date":"2024-02-16T07:52:25","request-id":"f5c6afb6-1435-4eee-8bc4-8987ef335eed","client-request-id":"f5c6afb6-1435-4eee-8bc4-8987ef335eed"}}}
   
  1. For /users/{userId}/chats, I receive a Forbidden error with the message "No authorization information present on the request."
   {"error":{"code":"Forbidden","message":"No authorization information present on the request.","innerError":{"date":"2024-02-16T07:52:26","request-id":"0a73d36b-cf32-47cc-9a31-9aa41fb2afe3","client-request-id":"0a73d36b-cf32-47cc-9a31-9aa41fb2afe3"}}}
   

Also, here are the scopes I'm using for the permissions (defined in my Application).

  • Channel.ReadBasic.All
  • ChannelMessage.Send
  • Chat.Create
  • Chat.Read
  • Chat.ReadBasic
  • Chat.ReadWrite
  • Chat.ReadWrite.All
  • ChatMember.Read
  • ChatMessage.Read
  • ChatMessage.Send
  • Directory.AccessAsUser.All
  • Directory.Read.All
  • profile
  • User.Read
  • User.Read.All
  • User.ReadBasic.All
Microsoft Security Microsoft Graph
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Akash Jadav 85 Reputation points Microsoft External Staff
    2024-02-18T05:18:59.78+00:00

    Hi, Can you please this tutorial for implementation reference:

    https://learn.microsoft.com/en-us/graph/tutorials/python?tabs=aad

    Can you also check these permissions are assigned are Delegate or Application Permissions.

    Let me know if you still face the issue.

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    0 comments No comments

  2. CarlZhao-MSFT 46,366 Reputation points
    2024-02-19T06:16:21.31+00:00

    Hi @BugBug IT

    This is the expected error. The /users endpoint and /users/{userId}/chats endpoint do not support personal or guest accounts. Please log in with a work account that has an Office 365 license and obtain an access token.

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional questions about this answer, please click Comment.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.