Unable to download the file from SharePoint using GraphServiceClient

Nandha-4804 0 Reputation points
2023-12-31T14:56:29.5233333+00:00

I am working on an Azure Function to download a file from a SharePoint Online Library.

I am using the Microsoft Graph to achieve this. I am using the ClientSecretCredential for authentication.

Microsoft.Graph Version: 5.37.0

.Net Version: 6.0

Note: The below line of code working fine, and I am able to get the Document details like URL, Created Time .......

            var test = await graphClient.Drives["*****"].Items["*******"].ListItem.GetAsync();

Note: The below line of code failing with an exception "The proxy tunnel request to proxy 'http://****/' failed with status code '407'"

But the same code is working If I used in the console application.

  var proxyAddress = "http://******.****.***:*****/";
          
            var handler = new HttpClientHandler
            {
                Proxy = new WebProxy(proxyAddress),
            };

         
            var options = new ClientSecretCredentialOptions()
            {
                Transport = new HttpClientTransport(handler),
            };
         
            // Values from app registration
            var clientId = "";
            var tenantId = "";
            var clientSecret = "";
            var tokenCredential = new ClientSecretCredential(
               tenantId,
                clientId,
                clientSecret,
                options);
            var authProvider = new AzureIdentityAuthenticationProvider(tokenCredential);

            var httpClient = GraphClientFactory.Create(proxy: new WebProxy(proxyAddress));
            var graphClient = new GraphServiceClient(httpClient, authProvider);
            var test = await graphClient.Drives["*****"].Items["*******"].ListItem.GetAsync();
            var stream = await graphClient.Drives["*******"].Items["********"].Content.GetAsync();
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,049 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
11,205 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Sourabh Gupta 800 Reputation points Microsoft Vendor
    2024-01-14T13:46:45.4233333+00:00

    Hi Nandha-4804 Thanks for reaching out. Error code 407 is coming because the request is lacking authentication credentials for the proxy server. You can use the following code to pass credentials.

    and use the httpClient instance created below while creating instance of GraphClient.

        ICredentials credentials = new NetworkCredential(username, password);
    
        var httpClient = GraphClientFactory.Create(proxy:
    										 new WebProxy(proxyAddress, true, null, credentials));
    
    

    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

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.