I am trying to upload file to sharepoint via Postman. I have following settings:
URL:
https://{tenantID}.sharepoint/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."
}
}
I am trying to upload file to sharepoint via Postman. I have following settings:
URL:
https://{tenantID}.sharepoint/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."
}
}
Share
Improve this question
edited Mar 25 at 10:10
Christian Baumann
3,5534 gold badges24 silver badges45 bronze badges
asked Mar 25 at 8:51
potatopotato
794 bronze badges
4
- Could you share the screenshot of your API permission blade and edit the question? – Pratik Jadhav Commented Mar 25 at 9:10
- Refer this Microsoft QnA Blog and let me know it works for you or not? @potato – Pratik Jadhav Commented Mar 25 at 10:47
- Please check below solution! – Pratik Jadhav Commented Mar 26 at 8:42
- Any update on the issue? @potato – Pratik Jadhav Commented Mar 28 at 5:33
2 Answers
Reset to default 0I got the same error message, When I try to upload the file using SharePoint REST API :
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:
Configured Authentication tab like below:
Use below PowerShell script :
Connect-PnPOnline {tenant}.sharepoint -Interactive -ClientID <application_id>
Grant-PnPAzureADAppSitePermission -AppId "<client-id>" -Site "https://{tenant}.sharepoint/sites/{your-site}" -Permissions FullControl
Using delegated type, authorization_code flow which requires user-interaction. To get code
, I ran below authorization request in browser:
https://login.microsoftonline/{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/.default
&state=12345
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/<tenant-id>/oauth2/v2.0/token
Body: FormEncoded
client_id: <application-id>
scope: https://{tenant}.sharepoint/.default
grant_type: authorization_code
code: <authorization_code generated from browser>
redirect_uri: http://localhost:58310
Response:
Now, Use below endpoints to upload the .png
file:
POST https://{tenantID}.sharepoint/sites/{mySite}/_api/web/GetFolderByServerRelativeUrl('/sites/{mySite}/Documents')/Files/add(url='<YOUR_FILE>',overwrite=true)
Response:
Also, I've verified from SharePoint App, .png
is uploaded successfully.
Reference:
Connect-PnPOnline Blog
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/sharepoint-online/in-4-steps-access-sharepoint-online-data-using-postman-tool/