0

I managed to get the access token in postman (After registering the app on Azure on following the required steps), but the permission for the app is set to only allow logged in users to read from Sharepoint. I can download using the rest api in the browser (where i am already logged in), but how do i successfully log in using the rest api in postman or C#, since get 'Status 403 forbidden' just using the access token (which is expected) as the file is only available for some users on Sharepoint.

Setting username and password in the body of the request does to work for me.

1 Answer 1

0

Actually, no need to register Azure AD App, instead register SharePoint App in /_layouts/15AppRegNew.aspx:

enter image description here

And then set the related permission in:

https://TenantName-admin.sharepoint.com/_layouts/15/appinv.aspx

enter image description here

Then use OfficeDevPnP to download file:

using Microsoft.SharePoint.Client;
using OfficeDevPnP.Core;
using System.IO;


string siteUrl = "https://tenant.sharepoint.com/";
using (var ctx = new AuthenticationManager().GetAppOnlyAuthenticatedContext(siteUrl, "yourappid", "yourappsecret"))
{
    ctx.Load(ctx.Web, p => p.Title);
    ctx.ExecuteQuery();
    Console.WriteLine(ctx.Web.Title);
    Microsoft.SharePoint.Client.File file = ctx.Web.GetFileByUrl("https://tenant.sharepoint.com/Shared%20Documents/test.txt");
    ctx.Load(file);
    ctx.ExecuteQuery();
    string filepath = @"C:\temp\" + file.Name;
    Microsoft.SharePoint.Client.ClientResult<Stream> mstream = file.OpenBinaryStream();
    ctx.ExecuteQuery();

    using (var fileStream = new System.IO.FileStream(filepath, System.IO.FileMode.Create))
    {
        mstream.Value.CopyTo(fileStream);
    }

};

Detailed steps to register SharePoint App, please refer the official guidence below:

Granting access using SharePoint App-Only

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.