Getting 403 Forbidden while Uploading file To Sharepoint

Mehak Talwar 0 Reputation points
2024-02-13T18:03:15.34+00:00

I am using SharePoint REST API v1 to upload a file to Sharepoint and getting the below error. I am able to get file using same credentials and auth mechanism. There is no issue at permissions level. The particlar user is able to upload file using UI console but the request is failing when performing same action using API. Request:' curl --location 'https://<site>/_api/web/GetFolderByServerRelativeUrl('''<Folder Path>''')/Files/add(url='''a.txt''',overwrite=true)'
--header 'Authorization: <Form Digest Token>'
--header 'Cookie: rtFa=<cookie value>'
--header 'Cookie: FedAuth=<cookie value>' --header 'Cookie: stsservicecookie=<cookie value>'
--header 'X-RequestDigest: Bearer <Form Digest Token>'
--header 'Content-Type: text/plain'
--data '{Hello World}'

<?xml version="1.0" encoding="utf-8"?>
<m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code>-2130575251, System.Runtime.InteropServices.COMException</m:code>
    <m:message xml:lang="en-US">The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.</m:message>
</m:error>

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2024-02-14T03:04:06.2233333+00:00

    Hi @Mehak Talwar,

    Per the offical document, you will need to get accessToken to upload a file.

    POST https://{site_url}/_api/web/GetFolderByServerRelativeUrl('Folder Name')/Files/Add(url='{file_name}', overwrite=true) 
    Authorization: "Bearer " + accessToken 
    Content-Length: {length of request body as integer} 
    X-RequestDigest: "{form_digest_value}"
    
    
    

    Please refer to following document https://learn.microsoft.com/en-us/sharepoint/dev/sp-add-ins/working-with-folders-and-files-with-rest#working-with-large-files-by-using-rest And you could refer to following steps to get accessToken https://www.inkeysolutions.com/blogs/access-sharepoint-online-data-part-1/

    ---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 Answers by the question author, which helps users to know the answer solved the author's problem.