Is it possible to access user's OneDrive using MSAL app only authentication for SPOnline REST API?

AdamMiosz-6301 1 Reputation point
2022-12-13T10:03:18.123+00:00

Hi,

I'm trying to access personal path to user's OneDrive using MSAL authorization and app only (App registration) permissions with SPOnline REST API.
Example personal path:

https://{tenant_name}/personal/{user_UPN}

I have App registration configured for my tenant with Application permissions with Sites.FullControl.All permissions that suppose to give my app full control to all site collections (so I assume OneDrive personal paths as well):
269906-image.png

But when I try to access the personal path, I get this error:

The remote server returned an error: (401) Unauthorized.

The code I use is:

 ClientContext ctx = new ClientContext(webFullUrl);  
 ctx.ExecutingWebRequest += (s, e) =>  
 {  
         e.WebRequestExecutor.RequestHeaders["Authorization"] = "Bearer " + userAccessToken;  
 };  
  
 using (ctx)  
 {  
         //Get context web  
         Web web = ctx.Web;  
         ctx.Load(  
                 web,  
                 w => w.EffectiveBasePermissions,  
                 w => w.CurrentUser);  
  
         //Get sp list";  
         List list = web.Lists.GetById(listId); //listId received other way  
         ctx.Load(list);  
         ctx.Load(list.Fields);  
         ctx.Load(list.UserCustomActions);  
         ctx.ExecuteQuery(); //**HERE I GET 401 ERROR when I try for OneDrive file (personal drive), for other sites it works fine!**  
 }  

Is it possible to access OneDrive personal path this way?

BR,
Adam

Microsoft 365 and Office | SharePoint | Development
Microsoft 365 and Office | SharePoint | For business | Windows
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,496 Reputation points Microsoft External Staff
    2022-12-14T02:03:21.203+00:00

    Hi @AdamMiosz-6301
    Per my reearch, rest api to get list and listitems doesn't support personal account
    270281-image.png
    So we are unable to access OneDrive personal path by rest api.
    Here is the document for more details
    https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/list_get?view=odsp-graph-online
    https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/listitem_get?view=odsp-graph-online


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.



Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.