Trying to upload files to sharepoint using REST API ,Python using Postman

Kompal Sithta 71 Reputation points
2022-12-01T07:13:44.49+00:00

import requests

url = "https://xxxxx.sharepoint.com/sites/QE/_api/Web/GetFolderByServerRelativeUrl('/sites/QE/Shared Documents/product-xxxxx-metrics')/Files/Add(url='test.txt', overwrite=true)"

payload="C:/Users/xxxxx/Downloads/xxxxxxxxs-open-20221201.csv"
headers = {
'Authorization': 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsIng1dCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSSIsImtpZCI6IjJaUXBKM1VwYmpBWVhZR2FYRUpsOGxWMFRPSxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx_S-IIN01oDEEAQQ',
'Accept': 'application/json;odata=verbose',
'X-RequestDigest': '0xBA800B078A8A918F3E8F5517B945D190B775C6A5D3BB1E949B35F66AEB52F34CDE26B30339ECD51729565EA0C394B3E436C7FFCB2DF003C765BB1B09DF601A64',
'Content-Type': 'text/csv'
}

response = requests.request("POST", url, headers=headers, data=payload)

print(response.text)

Hi,
This is code that I am trying to implement to upload file on sharepoint but I am facing an error while running the code.
The error is as follows:

code":"-2147024891, System.UnauthorizedAccessException","message":{"lang":"en-US","value":"Access denied.}

But I have given all the permissions required.

Microsoft 365 and Office SharePoint Development
Microsoft 365 and Office SharePoint Server Development
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2022-12-02T02:28:09.463+00:00

    Hi @Kompal Sithta
    I will recommend you to use Office365-Rest-Python-Client to consume SharePoint Rest API and it supported App-Only Credential authentication (AuthenticationContext.ctx_auth.acquire_token_for_app(client_id, client_secret)):
    Office365-REST-Python-Client 2.3.16

    Here is a sample to connect SharePoint Online using App Only credential:

     site_url = 'https://contoso.sharepoint.com/'  
     app_principal = {  
         'client_id': '--client-id-goes-here--',  
         'client_secret': '--client-secret-goes-here--',  
     }  
          
     context_auth = AuthenticationContext(url=site_url)  
     context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])  
          
     ctx = ClientContext(site_url, context_auth)  
     web = ctx.web  
     ctx.load(web)  
     ctx.execute_query()  
     print("Web site title: {0}".format(web.properties['Title']))  
    

    This document provide more details
    https://github.com/vgrem/Office365-REST-Python-Client


    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.