OneDrive SharedWithMe not working with Client Secret getting 500 generalException response

Sachin Giri 0 Reputation points
2025-01-14T11:19:12.2933333+00:00

Hi,

I am using the Microsoft Graph API to fetch the OneDrive SharedWithMe items. I'm using Client Secret Flow for authentication. Below is the relevant part of my code. However, when I execute the SharedWithMe API, I receive a 500 generalException error. All other API's are working fine. And below code was also working till last week. Suddenly something happened and I start receiving the 500 General Exception. Could anyone help me understand what is the issue or do I missing something?

Code for Get Shared With Me Items:

public async Task<List<Tuple<string, SharedWithMeResponse?>>> GetSharedDriveItemsAsync(GraphServiceClient microsoftGraphClientContext, List<string?> driveIds)
{
    try
    {
        List<Tuple<string, SharedWithMeResponse?>> driveItemCollection = new List<Tuple<string, SharedWithMeResponse?>>();
        var batchRequestContent = new BatchRequestContent(microsoftGraphClientContext);
        List<string> requestIds = new List<string>();
        foreach (var driveId in driveIds)
        {
            var request = microsoftGraphClientContext
                   .Drives[driveId]
                   .SharedWithMe;
            var driveItemsRequestInformation = new RequestInformation
            {
                HttpMethod = Method.GET,
                UrlTemplate = request.ToGetRequestInformation().URI.ToString()
            };
            driveItemsRequestInformation.AddRequestOptions(new List<IRequestOption> { retryHandlerOption });
            var driveItemsPageRequestMessage = await microsoftGraphClientContext.RequestAdapter.ConvertToNativeRequestAsync<HttpRequestMessage>(driveItemsRequestInformation);
            //HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Get, driveItemsRequestInformation.URI.ToString());
            BatchRequestStep batchRequestStep = new BatchRequestStep(driveId, driveItemsPageRequestMessage);
            batchRequestContent.AddBatchRequestStep(batchRequestStep);
        }
        var returnedResponse = await microsoftGraphClientContext.Batch.PostAsync(batchRequestContent);
        if (returnedResponse != null)
        {
            foreach (var driveId in driveIds)
            {
                SharedWithMeResponse? sharedDriveItemCollectionResponse = await returnedResponse
                    .GetResponseByIdAsync<SharedWithMeResponse>(driveId);
                if (sharedDriveItemCollectionResponse != null)
                {
                    driveItemCollection.Add(new Tuple<string, SharedWithMeResponse?>(driveId, sharedDriveItemCollectionResponse));
                }
            }
        }
        return driveItemCollection;
    }
    catch (Exception exception)
    {
        throw;
    }
}

Code for Microsoft Graph Service Client:

public class ServiceClient
{
    public GraphServiceClient microsoftGraphServiceClient;
    public ServiceClient(string clientId, string clientSecret, string tenantId)
    {
        var scopes = new string[] { "https://graph.microsoft.com/.default" };
        var options = new TokenCredentialOptions
        {
            AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
        };
        var clientSecretCredential = new ClientSecretCredential(tenantId, clientId, clientSecret, options);
        microsoftGraphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);
    }
}

Error:User's image

"CorrelationId": "XXX",
  "IsSuccessful": false,
  "StatusCode": 500,
  "Messages": [
    "Exception occured API processing for CorrelationId = XXX",
    "generalException"
  ],
  "Data": "   at Microsoft.Graph.ResponseHandler`1.ValidateSuccessfulResponse(HttpResponseMessage httpResponseMessage, Dictionary`2 errorMapping)\r\n   at Microsoft.Graph.ResponseHandler`1.HandleResponseAsync[NativeResponseType,ModelType](NativeResponseType response, Dictionary`2 errorMappings)\r\n   at Microsoft.Graph.BatchResponseContent.GetResponseByIdAsync[T](String requestId, IResponseHandler responseHandler)
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Akhil Nasalwai - MSFT 1,685 Reputation points Microsoft External Staff
    2025-01-14T17:08:07.5933333+00:00

    Hello Sachin Giri,

    Thank you for contacting Microsoft!

    Regarding the Graph API that is being used, as per the official documentation reference below, the API does not support application permissions, as the endpoint has me/drive, only delegated permissions are supported. Please let us know if the use case is different from the API mentioned in the documentation. Sometimes the error 500 might be related to server side, we would recommend you to try executing the code after some time or test the Graph API using Graph Explorer and let us know the response.

    Link : https://learn.microsoft.com/en-us/graph/api/drive-sharedwithme?view=graph-rest-1.0&tabs=http#permissions

    User's image

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.


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.