-1

I am trying to upload file to sharepoint via Postman. I have following settings:

URL:

https://{tenantID}.sharepoint.com/sites/{mySite}/_api/web/GetFolderByServerRelativeUrl('/sites/{mySite}/Shared Documents')/Files/add(url='test.png',overwrite=true)

Headers:

Authorization: Bearer {accessToken}
Accept: application/json;odata=verbose
Content-Type: application/octet-stream

uploaded file of type .png in binary form of Postman

Response:

 "error": {
        "code": "-2147024891, System.UnauthorizedAccessException",
        "message": {
            "lang": "en-US",
            "value": "Access denied."
        }
    }
1
  • Refer this Microsoft QnA Blog and let me know it works for you or not? @potato Commented Mar 25 at 10:47

2 Answers 2

0

I got the same error message, When I try to upload the file using SharePoint REST API :

enter image description here

To resolve the error, you have to add permission Full Control permission to your application.

Initially, I registered Single-Tenant Microsoft Entra ID Application and Added delegated type API permission like below and granted admin consent:

enter image description here

Configured Authentication tab like below:

enter image description here

Use below PowerShell script :

Connect-PnPOnline {tenant}.sharepoint.com -Interactive -ClientID <application_id>

Grant-PnPAzureADAppSitePermission -AppId "<client-id>" -Site "https://{tenant}.sharepoint.com/sites/{your-site}" -Permissions FullControl

enter image description here

Using delegated type, authorization_code flow which requires user-interaction. To get code, I ran below authorization request in browser:

https://login.microsoftonline.com/{Tenant-ID}/oauth2/v2.0/authorize?  
client_id=<CLIENT_ID>
&response_type=code  
&redirect_uri=http://localhost:58310
&response_mode=query  
&scope=https://{tenant}.sharepoint.com/.default
&state=12345

enter image description here

After successfully creating authorization_code, Generated access token using below parameters:

Headers:

Accept: application/json;odata=verbose
Content-Type: application/octet-stream

Body:

POST https://login.microsoftonline.com/<tenant-id>/oauth2/v2.0/token
Body: FormEncoded

client_id: <application-id>
scope: https://{tenant}.sharepoint.com/.default
grant_type: authorization_code
code: <authorization_code generated from browser>
redirect_uri: http://localhost:58310

Response:

enter image description here

Now, Use below endpoints to upload the .png file:

POST https://{tenantID}.sharepoint.com/sites/{mySite}/_api/web/GetFolderByServerRelativeUrl('/sites/{mySite}/Documents')/Files/add(url='<YOUR_FILE>',overwrite=true)

Response:

enter image description here

Also, I've verified from SharePoint App, .png is uploaded successfully.

enter image description here

Reference:

Connect-PnPOnline Blog

Sign up to request clarification or add additional context in comments.

Comments

0

You need to register an app in SharePoint in order to authenticate against it. That app that you register needs to be given permissions to your SharePoint Online.

Here is the document for grant access to SharePoint app only

https://global-sharepoint.com/sharepoint-online/in-4-steps-access-sharepoint-online-data-using-postman-tool/

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.