Sharepoint teamsite REST API

David CPM 1 Reputation point
2022-03-22T13:53:39.977+00:00

I'm trying to upload a file to the company's teamsite using the SharePlum python library but am getting a permission error:

HTTPError: 403 Client Error: Forbidden for url: https://********.sharepoint.com/teams/teamsite/_vti_bin/lists.asmx

My gut instict is telling me that this is because I am trying to connect to a teamsite rather than a site as ALL examples show.

This person has the same problem:

https://stackoverflow.com/questions/65098933/python-upload-local-file-to-sharepoint-team-site
If it's not to do with it being a team site, then I must not have sufficient permission?

But I am able to get the auth cookies with the following:

authcookie = Office365(url, username=user, password=password).GetCookies()

But am unable to connect to the site with:

site_ = Site(site, version=Version.v365, authcookie=authcookie)

I have tried using different versions.

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
    2022-03-23T02:33:46.183+00:00

    Hi @David CPM ,
    You can refer to following code to upload file.

     from shareplum import Office365  
     from shareplum import Site  
     from shareplum.site import Version  
          
     authcookie = Office365('https://TenantName.sharepoint.com', username='******@TenantName.onmicrosoft.com', password='******').GetCookies()  
     site = Site('https://TenantName.sharepoint.com/sites/SiteName/', version=Version.v365, authcookie=authcookie);  
     folder = site.Folder('Shared Documents')  
     with open("D:\myfile.txt", mode='rb') as file:  
             fileContent = file.read()  
     folder.upload_file(fileContent, "myfile.txt")  
    

    You could also use Office365-Rest-Python-Client library to access to Sharepoint documents. https://github.com/vgrem/Office365-REST-Python-Client

    Here are some examples about file operations: https://github.com/vgrem/Office365-REST-Python-Client/tree/master/examples/sharepoint/files

    For SharePoint Online, please check if the user account which enabled MFA, then it's not supported, try to close MFA, you can check the detailed Conditional Access Policy as official document:

    Troubleshooting sign-in problems with Conditional Access


    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.