Skip to content

Errors when trying to create a GraphServiceClient #2817

@manojkapoor-eptura

Description

@manojkapoor-eptura
    private GraphServiceClient InitializeServiceForMSGraph(string accessTokenFromHeader)
    {
        // Your existing code to extract the access token from the request headers
        var accessToken = accessTokenFromHeader.Substring("Bearer ".Length);

        // Create a TokenCredential using the access token
        var tokenCredential = new **AccessTokenCredential**(accessToken);

        // Initialize the GraphServiceClient with the TokenCredential
        var graphServiceClient = new GraphServiceClient((tokenCredential);

        return graphServiceClient;
       }

With the above code, its unable to find a reference to AccessTokenCredential.

Second approach tried was as below

        var authProvider = new DelegateAuthenticationProvider(async (requestMessage) =>
        {
           // Set the Authorization header with the access token
            requestMessage.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
        });

        var appGraphClient = new GraphServiceClient(authProvider);

        return appGraphClient;

With this approach, I am getting an error on DelegateAuthenticationProvider which seems to have being deprecated by Microsoft

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions