Unable to upload files to sharepoint using App only authentication.

stramzik 161 Reputation points
2022-02-01T07:59:14.583+00:00

I am trying to upload files to Sharepoint via a Python code. I can use my personal AD credentials to authenticate using the below code and it works fine to upload files to Sharepoint.

from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    import os 
    from office365.runtime.auth.user_credential import UserCredential
    from office365.runtime.auth.client_credential import ClientCredential

    baseurl = 'https://company.sharepoint.com/'
    basesite = '/sites/testsitename' # every share point has a home.
    siteurl = baseurl + basesite 

    localpath = r"C:\Users\Desktop\testfolder\pandas_table.xlsx"
    remotepath = "Shared Documents/testfolder/file.xlsx" # existing folder path under sharepoint site.

    ctx_auth = AuthenticationContext(siteurl)
    ctx_auth.acquire_token_for_user("username", "password")
    ctx = ClientContext(siteurl, ctx_auth)

    with open(localpath, 'rb') as content_file:
        file_content = content_file.read()

    dir, name = os.path.split(remotepath)
    file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()

I had an app registered from our Cloud team and added necessary permission on the Service principle to write files using the Service principles Client ID and Client secret. I modified the above code using the Client Credentials based on the Office365 Python documentation and The web object returns empty json. Can somebody please tell me what I am doing wrong?

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
import os 
from office365.runtime.auth.user_credential import UserCredential
from office365.runtime.auth.client_credential import ClientCredential

baseurl = 'https://company.sharepoint.com/'
basesite = '/sites/testsitename' # every share point has a home.
siteurl = baseurl + basesite 

localpath = r"C:\Users\Desktop\testfolder\pandas_table.xlsx"
remotepath = "Shared Documents/testfolder/file.xlsx" # existing folder path under sharepoint site.

client_credentials = ClientCredential("{cliend-id}","{cliend-secret}")
ctx = ClientContext(siteurl).with_credentials(client_credentials)

with open(localpath, 'rb') as content_file:
    file_content = content_file.read()

dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,560 questions
{count} vote

Accepted answer
  1. RaytheonXie_MSFT 30,906 Reputation points Microsoft Vendor
    2022-02-02T06:08:10.04+00:00

    Hi @stramzik ,
    I will recommend you to grant access using SharePoint App-Only by the document you have provided. Before you grant Access you could run this cmdlet first Set-SPOTenant -DisableCustomAppAuthentication $false.For new SharePoint subscription Grant App Permission is disabled by default or the browser link https://xxxx-admin.sharepoint.com/_layouts/15/appinv.aspx is disabled. To enable this feature, we need to connect to SharePoint using Windows PowerShell and then run the cmdlet.

     Install-Module -Name Microsoft.Online.SharePoint.PowerShell  
     $adminUPN="<the full email address of a SharePoint administrator account, example: jdoe@contosotoycompany.onmicrosoft.com>"  
     $orgName="<name of your Office 365 organization, example: contosotoycompany>"  
     $userCredential = Get-Credential -UserName $adminUPN -Message "Type the password."  
     Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $userCredential  
     set-spotenant -DisableCustomAppAuthentication $false  
    

    Afterward, you can follow the document steps to grant 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.



0 additional answers

Sort by: Most helpful