msgraph-beta-sdk-python icon indicating copy to clipboard operation
msgraph-beta-sdk-python copied to clipboard

How to use authentication user flows?

Open pamelafox opened this issue 10 months ago • 1 comments

I am trying to figure out how to get and create user flows. This is as far as I've figured out the code for getting a userflow by a filtered request:

  async def get_userflow(graph_client_beta: GraphServiceClient, identifier: str) -> (str | None):
      app_name = f"ChatGPT Sample User Flow {identifier}"
  
      query_params = AuthenticationEventsFlowsRequestBuilder.AuthenticationEventsFlowsRequestBuilderGetQueryParameters(
          filter=f"displayName eq '{app_name}'",
      )
      request_configuration = RequestConfiguration(
          query_parameters=query_params,
      )
      result = await graph_client_beta.identity.authentication_events_flows.get(request_configuration=request_configuration)
      if result.value:
          return result.value[0].id
      return None

With reference to this doc:

https://learn.microsoft.com/en-us/graph/api/authenticationeventsflow-get?view=graph-rest-beta&viewFallbackFrom=graph-rest-1.0&tabs=python

However, that code errors:

  File "/Users/pamelafox/openai-chat-app-quickstart/./scripts/auth_init.py", line 157, in get_userflow
    result = await graph_client_beta.identity.authentication_events_flows.get(request_configuration=request_configuration)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'IdentityRequestBuilder' object has no attribute 'authentication_events_flows'

I've also tried graph_client_beta.authentication_events_flows, but that does not exist.

Could you advise? I am having a difficult time navigating the source code to figure it out. Thanks!

pamelafox avatar Apr 19 '24 23:04 pamelafox

This is happening on both the V1 SDK and the Beta SDK, with error

 result = await client.identity.authentication_events_flows.get()
AttributeError: 'IdentityRequestBuilder' object has no attribute 'authentication_events_flows'

The Request builders exist in the path

from msgraph.generated.identity.authentication_events_flows.authentication_events_flows_request_builder import AuthenticationEventsFlowsRequestBuilder

For V1 and:

from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.identity.authentication_events_flows.authentication_events_flows_request_builder import AuthenticationEventsFlowsRequestBuilder

But seems cannot be imported, other paths can be imported fine though: e.g

from msgraph_beta.generated.groups.groups_request_builder import GroupsRequestBuilder

Also checking to see if there are more paths that are available in the generated code, but cannot be imported.

shemogumbe avatar May 03 '24 10:05 shemogumbe

After investigation, I have established that generation is fine:

  • The problem is most likely that you are using a v1 client to execute a feature only available on beta. Code causing Error
from msgraph import GraphServiceClient
from msgraph_beta.generated.identity.authentication_events_flows.authentication_events_flows_request_builder import AuthenticationEventsFlowsRequestBuilder

scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credentials, scopes)

app_name = "Shem Test app"


async def get_auth_flows():
    try:
        query_params = AuthenticationEventsFlowsRequestBuilder.AuthenticationEventsFlowsRequestBuilderGetQueryParameters(
            filter=f"displayName eq '{app_name}'", )
        request_configuration = RequestConfiguration(
            query_parameters=query_params)
        result = await graph_client.identity.authentication_events_flows.get(
            request_configuration=request_configuration)

        if result.value:
            return result.value[0].id
        return None
    except APIError as e:
        print(f"Error - {e}")


asyncio.run(get_auth_flows())

The error:

Traceback (most recent call last):
  File "C:\Users\shemogumbe\kiota_debug\src\auth_flows.py", line 39, in <module>
    asyncio.run(get_auth_flows())
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\asyncio\base_events.py", line 649, in run_until_complete
    return future.result()
  File "C:\Users\shemogumbe\kiota_debug\src\auth_flows.py", line 29, in get_auth_flows
    result = await graph_client.identity.authentication_events_flows.get(
AttributeError: 'IdentityRequestBuilder' object has no attribute 'authentication_events_flows'. Did you mean: 'authentication_event_listeners'?

Code without the Error:

from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.identity.authentication_events_flows.authentication_events_flows_request_builder import AuthenticationEventsFlowsRequestBuilder

scopes = ['https://graph.microsoft.com/.default']
graph_client = GraphServiceClient(credentials, scopes)

app_name = "Shem Test app"


async def get_auth_flows():
    try:
        query_params = AuthenticationEventsFlowsRequestBuilder.AuthenticationEventsFlowsRequestBuilderGetQueryParameters(
            filter=f"displayName eq '{app_name}'", )
        request_configuration = RequestConfiguration(
            query_parameters=query_params)
        result = await graph_client.identity.authentication_events_flows.get(
            request_configuration=request_configuration)

        if result.value:
            return result.value[0].id
        return None
    except APIError as e:
        print(f"Error - {e}")


asyncio.run(get_auth_flows())

Has a different issue:

Error -
        APIError
        Code: 403
        message: None
        error: MainError(additional_data={}, code='AADB2C', details=None, inner_error=InnerError(additional_data={'correlationId': UUID('1828442e-3845-4058-9455-18941734d8f0')}, client_request_id='3a112ab8-3655-4731-86c0-462193962879', date=DateTime(2024, 5, 22, 12, 5, 50, tzinfo=Timezone('UTC')), odata_type=None, request_id='da23b5ac-00b5-4f02-82d7-fba0282ada8e'), message="Unauthorized. Access to this Api requires feature: 'EnableMsGraphAuthenticationEventListener' for the tenant: '80e28aad-cbaf-4d00-b758-b8032dd946be'.", target=None)

Considering this is fauture only available in beta and not in V1, the bug in the code that runs, from the API, makes sense, any view on this @pamelafox

shemogumbe avatar May 22 '24 12:05 shemogumbe

Ah, I think that's right! I created a new GraphServiceClient for those flows, but neglected to use the version imported from the beta client. I reverted the code to raw HTTP calls for now but will try out the GraphServiceClient from msgraph_beta soon. Thanks!

pamelafox avatar May 22 '24 12:05 pamelafox