Reputation: 87
I am trying to find the group ID from the display name of the group, using the Microsoft Graph API.
My code:
group = 'Test'
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
payload5 = {'Authorization':token,'Host':'graph.microsoft.com','ConsistencyLevel':'eventual'}
https = urllib3.PoolManager()
response5 = http.request('GET', uri5, headers=payload5)
mydict5 = eval(response5.data)
groupID = f"{mydict5['id']}"
I receive the error:
{'error': {'code': 'Request_UnsupportedQuery', 'message': 'Unsupported Query.'}
I am not sure what I am missing here. Any and all help is greatly appreciated!
Upvotes: 1
Views: 324
Reputation: 22542
I tried to reproduce the same in my environment and got the below results:
I have created one Azure AD group named Test
like below:
I created one Azure AD application and granted permissions as below:
Now, I ran the same code as you from previous question and got the token
successfully as below:
import urllib3
uri = "https://login.microsoftonline.com/tenantID/oauth2/v2.0/token"
payload= {
'Content-Type': 'application/x-www-form-urlencoded',
'Host': 'login.microsoftonline.com',
'client_id': 'xxxxxxxxxxxxxxxxxxxxx',
'scope': 'https://graph.microsoft.com/.default',
'client_secret': 'xxxxxxxxxxxxxxxxxxxxx',
'grant_type': 'client_credentials'
}
http = urllib3.PoolManager()
response = http.request('POST', uri, payload)
my_dict = eval(response.data)
token = f"{my_dict['token_type']} {my_dict['access_token']}"
print(token)
Output:
When I ran the same code to get the group, I got the same error as below:
group = 'Test'
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
payload5 = {'Authorization':token,'Host':'graph.microsoft.com','ConsistencyLevel':'eventual'}
https = urllib3.PoolManager()
response5 = http.request('GET', uri5, headers=payload5)
print(response5.data)
#mydict5 = eval(response5.data)
#groupID = f"{mydict5['id']}"
Output:
To resolve the error, try to modify your code like below:
group = "'Test'"
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
payload5 = {'Authorization':token,'Host':'graph.microsoft.com','ConsistencyLevel':'eventual'}
https = urllib3.PoolManager()
response5 = http.request('GET', uri5, headers=payload5)
print(response5.data)
#mydict5 = eval(response5.data)
#groupID = f"{mydict5['id']}"
When I ran the above modified code, I got the response with groupID
successfully like below:
UPDATE
I agree with user2250152, and got groupID
running same code as below:
group = "'Test'"
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
payload5 = {'Authorization':token,'Host':'graph.microsoft.com','ConsistencyLevel':'eventual'}
https = urllib3.PoolManager()
response5 = http.request('GET', uri5, headers=payload5)
#print(response5.data)
mydict5 = json.loads(response5.data)
groupID = f"{mydict5['value'][0]['id']}"
print(groupID)
Output:
Upvotes: 1
Reputation: 20768
It's more python issue caused by wrong formatting.
group = 'Test'
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
When you try to print uri5
the result is
https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq Test
As you can see single quotes are missing after eq
operator. You can either wrap it in doubles quotes
group = "'Test'"
uri5 = 'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq ' + group
or use formatting string with F-Strings
group = 'Test'
uri5 = f'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq \'{group}\''
Result:
https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq 'Test'
The response data response5.data
is json string. You can import json package and read the value like this
import json
group = 'Test'
uri5 = f'https://graph.microsoft.com/v1.0/groups?$count=true&$filter=displayName eq \'{group}\''
payload5 = {'Authorization':token,'Host':'graph.microsoft.com','ConsistencyLevel':'eventual'}
https = urllib3.PoolManager()
response5 = http.request('GET', uri5, headers=payload5)
mydict5 = json.loads(response5.data)
groupID = f"{mydict5['value'][0]['id']}"
Upvotes: 1