我引用了https://learn.microsoft.com/en-us/graph/tutorials/python-app-only?tabs=aad。
复制并粘贴了确切的代码,但它不返回用户列表。它返回访问令牌。
main.py文件:-
import asyncio
import configparser
from msgraph.generated.models.o_data_errors.o_data_error import ODataError
from graph import Graph
async def main():
print('Python Graph App-Only Tutorial\n')
# Load settings
config = configparser.ConfigParser()
config.read(['config.cfg', 'config.dev.cfg'])
azure_settings = config['azure']
graph: Graph = Graph(azure_settings)
choice = -1
while choice != 0:
print('Please choose one of the following options:')
print('0. Exit')
print('1. Display access token')
print('2. List users')
print('3. Me')
try:
choice = int(input())
except ValueError:
choice = -1
try:
if choice == 0:
print('Goodbye...')
elif choice == 1:
await display_access_token(graph)
elif choice == 2:
await list_users(graph)
elif choice == 3:
await mee(graph)
else:
print('Invalid choice!\n')
except ODataError as odata_error:
print('Error:')
if odata_error.error:
print(odata_error.error.code, odata_error.error.message)
async def display_access_token(graph: Graph):
token = await graph.get_app_only_token()
print('App-only token:', token, '\n')
async def list_users(graph: Graph):
users_page = await graph.get_users()
print(users_page)
# Output each users's details
if users_page and users_page.value:
for user in users_page.value:
print('User:', user.display_name)
print(' ID:', user.id)
print(' Email:', user.mail)
# If @odata.nextLink is present
more_available = users_page.odata_next_link is not None
print('\nMore users available?', more_available, '\n')
async def mee(graph: Graph):
user = await graph.me()
async def make_graph_call(graph: Graph):
# TODO
return
# Run main
asyncio.run(main())
**---------------------------------------------------------------------------------------graph.py文件:-
from configparser import SectionProxy
from azure.identity.aio import ClientSecretCredential
from kiota_authentication_azure.azure_identity_authentication_provider import (
AzureIdentityAuthenticationProvider
)
from msgraph import GraphRequestAdapter, GraphServiceClient
from msgraph.generated.users.users_request_builder import UsersRequestBuilder
from kiota_abstractions.api_error import APIError
import asyncio
class Graph:
settings: SectionProxy
client_credential: ClientSecretCredential
adapter: GraphRequestAdapter
app_client: GraphServiceClient
def __init__(self, config: SectionProxy):
self.settings = config
client_id = self.settings['clientId']
tenant_id = self.settings['tenantId']
client_secret = self.settings['clientSecret']
self.client_credential = ClientSecretCredential(tenant_id, client_id, client_secret)
auth_provider = AzureIdentityAuthenticationProvider(self.client_credential) # type: ignore
self.adapter = GraphRequestAdapter(auth_provider)
self.app_client = GraphServiceClient(self.adapter)
async def get_app_only_token(self):
graph_scope = 'https://graph.microsoft.com/.default'
access_token = await self.client_credential.get_token(graph_scope)
return access_token.token
async def get_users(self):
try:
query_params = UsersRequestBuilder.UsersRequestBuilderGetQueryParameters(
# Only request specific properties
select = ['displayName', 'id', 'mail'],
# Get at most 25 results
top = 25,
# Sort by display name
orderby= ['displayName']
)
request_config = UsersRequestBuilder.UsersRequestBuilderGetRequestConfiguration(
query_parameters=query_params
)
users = await self.app_client.users.get(request_configuration=request_config)
return users
except APIError as e:
print(f'Error: {e.error.message}')
async def me(self):
try:
me = await self.app_client.me.get()
if me:
print(me)
except APIError as e:
print(f'Error: {e}')
1条答案
按热度按时间hpxqektj1#
我引用了https://learn.microsoft.com/en-us/graph/tutorials/python-app-only?tabs=aad。复制并粘贴了确切的代码,但它不返回用户列表。它返回访问令牌。
我在我的环境中尝试了相同的文档,即使对我来说,它也没有列出用户,但它返回访问令牌。
或者,您可以使用下面的代码来获取使用Python的用户列表。
代码:
输出: