Shared with me not working with Client Secret Flow

Dipika Das 141 Reputation points
2025-01-09T09:56:01.04+00:00

Hi,

I am using the Microsoft Graph 'Shared With Me' API to fetch all shared files using Client Secret Flow for authentication. Below is the relevant part of my code. However, when I execute the 'Shared With Me' API, I receive a Bad Request error (below image). All other APIs, such as 'Get Drive' and 'Drive Items', are working fine. Could you help me understand what might be causing this issue or if I’m missing something?

Working API:

DriveItem driveRoot = await graphClient.Drives[DriveId].Root.GetAsync();
DriveItemCollectionResponse driveItems = await graphClient.Drives[DriveId].Items[driveRoot.Id].Children.GetAsync((requestConfiguration) =>
{
    requestConfiguration.QueryParameters.Expand = new string[] { "thumbnails" };
});

Not Working API:

SharedWithMeResponse sharedItems = await graphClient.Drives[DriveId].SharedWithMe.GetAsync();

private GraphServiceClient graphClient;
 private void setGraphClientForAppAuth()
 {
     var options = new TokenCredentialOptions
     {
         AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
     };
     clientSecretCredential = new ClientSecretCredential(
        _teamsSettings.tenant, _teamsSettings.clientId, _teamsSettings.appkey, options);
     graphClient = new GraphServiceClient(clientSecretCredential);
 }

SharedWithMeResponse sharedItems = await graphClient.Drives[DriveId].SharedWithMe.GetAsync();

img1

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
12,769 questions
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,256 questions
SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,191 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vasil Michev 111.7K Reputation points MVP
    2025-01-09T16:56:09.7433333+00:00

    As mentioned in the documentation, this endpoint is not supported for application permissions (i.e. when authenticating via the client credentials flow): https://zcusa.951200.xyz/en-us/graph/api/drive-sharedwithme?view=graph-rest-1.0&tabs=http

    You need to authenticate in the context of the user owning the given OneDrive to fetch this.


  2. CarlZhao-MSFT 44,671 Reputation points
    2025-01-10T02:51:47.38+00:00

    Hi @Dipika Das

    The GET /me/drive/sharedWithMe API does not support application permissions, it is only available in a delegated context, which requires user involvement.

    User's image

    var scopes = new[] { "Files.ReadWrite.All" };
    var tenantId = "TENANT_ID";
    var clientId = "YOUR_CLIENT_ID";
    var clientSecret = "YOUR_CLIENT_SECRET";
    
    // For authorization code flow, the user signs into the Microsoft
    // identity platform, and the browser is redirected back to your app
    // with an authorization code in the query parameters
    var authorizationCode = "AUTH_CODE_FROM_REDIRECT";
    
    // using Azure.Identity;
    var options = new AuthorizationCodeCredentialOptions
    {
        AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
    };
    
    // https://zcusa.951200.xyz/dotnet/api/azure.identity.authorizationcodecredential
    var authCodeCredential = new AuthorizationCodeCredential(
        tenantId, clientId, clientSecret, authorizationCode, options);
    
    var graphClient = new GraphServiceClient(authCodeCredential, scopes);
    

    Hope this helps.

    If the reply is helpful, please click Accept Answer and kindly upvote it. If you have additional 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.