I'm working on this Python script to upload file on a SharePoint :

from requests_ntlm import HttpNtlmAuth
from pypac import PACSession, get_pac

session = PACSession(get_pac(url='mypac.pac'))

headers = {
    'Content-Type': 'application/json;odata=verbose',
    'Accept': 'application/json;odata=verbose',
username = 'username'
password = 'password'
url = "https://my.sharepoint.com/path/_api/contextinfo"
request_result = session.get(url,
                             auth=HttpNtlmAuth(username, password),

Despite the fact that I have full permissions and no problem occured when I try manually with browsers, I always get 403 Forbidden error with the following message :

"Access denied. You do not have permission to perform this action or access this resource."

This script works well on two others SharePoint but not on this one.

  • Check with the permission on the document library where you are uploading the file
    – Sunil Sahu
    Mar 26, 2018 at 13:13
  • Can you explain more please ? I am a SharePoint newbie... Where can I go to manage to do that ?
    – Darkaird
    Mar 26, 2018 at 13:43
  • Go to list/library setting --> under the "Permissions and Management" click on "Permissions for this list" and from the ribbon click on "Check permission" and type the user name you re using must have contribute permission
    – Sunil Sahu
    Mar 26, 2018 at 13:50
  • Is this a local installation of SharePoint or SharePoint Online / Office 365? "my.sharepoint.com" is suggesting SharePoint Online.
    – ben
    Mar 27, 2018 at 8:17

1 Answer 1


It looks like you're trying to connect to SharePoint Online (sharepoint.com). You have to use FedAuth instead of Ntlm to authenticate against SharePoint Online.

See Vadim Gremyachev's great answer at https://stackoverflow.com/questions/35187724/remote-authentication-in-sharepoint-online

tl;dr: Vadim has published a sample project for REST authentication with Python under https://github.com/vgrem/Office365-REST-Python-Client

  • Isn't it just possible to use a vanilla requests module instead ? Because the answers you show use extra module and I would want to keep vanilla requests.
    – Darkaird
    Mar 30, 2018 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.