I am using python-sharepoint to access data from my sharepoint account. But I always get a urllib2.HTTPError: HTTP Error 403: Forbidden
error. I am following this github link. Here is my code:
from sharepoint import SharePointSite, basic_auth_opener
server_url = 'https://myServerUrl.sharepoint.com/'
site_url = server_url + 'sites/test'
username = 'myUsername.onmicrosoft.com'
password = 'myPassword'
opener = basic_auth_opener(server_url, username, password)
site = SharePointSite(site_url, opener)
for sp_list in site.lists:
print (sp_list)
I also tried creating urllib2 opener as:
from sharepoint import SharePointSite
from ntlm import HTTPNtlmAuthHandler
import urllib2
password_manager = urllib2.HTTPPasswordMgrWithDefaultRealm()
password_manager.add_password(None, server_url, username, password)
auth_handler = HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(password_manager)
opener = urllib2.build_opener(auth_handler)
site = SharePointSite(site_url, opener)
for sp_list in site.lists:
print (sp_list)
But again it gives same error. Any solution will be appreciated.
Neither Basic nor NTLM authentication methods are not supported in SharePoint Online, which makes python-sharepoint not the best choice here.
Instead i would recommend to give a try Office365-REST-Python-Client which supports Office 365/SharePoint Online.
Here is the similar example for getting all the lists in a site and printing its title
ctx_auth = AuthenticationContext(url='https://contoso.sharepoint.com')
if ctx_auth.acquire_token_for_user(username='jdoe@contoso.onmicrosoft.com'],
password='--password--'):
ctx = ClientContext(settings['url'], ctx_auth)
lists = ctx.web.lists
ctx.load(lists)
ctx.execute_query()
for l in lists:
print(l.properties["Title"])