5

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),
                             headers=headers)
print(request_result)

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.

4
  • Check with the permission on the document library where you are uploading the file
    – Sunil Sahu
    Commented 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
    Commented 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
    Commented 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
    Commented Mar 27, 2018 at 8:17

1 Answer 1

3

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

1
  • 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
    Commented 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.