Issues while making connection to Shareporint Site

ASH2212 1 Reputation point
2022-12-27T05:36:07.963+00:00

While trying to make connection to SharePoint site using Office365-REST-Python-Client I keep getting Index out of range error. I am not sure of the reason.
My code is as below:

conn = ClientContext(SHAREPOINT_SITE).with_credentials(
UserCredential(
USERNAME,
PASSWORD
)
)

web = conn.web
conn.load(web)
conn.execute_query()
print("Site title: {0}".format(web.properties['Title']))

The error occurs as below:

---------------------------------------------------------------------------

IndexError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_29876\1444466101.py in <module>
1 web = conn.web
2 conn.load(web)
----> 3 conn.execute_query()
4 print("Site title: {0}".format(web.properties['Title']))

.
.
.
.
~\Anaconda3\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in _acquire_service_token_from_adfs(self, adfs_url)
138 headers={'Content-Type': 'application/soap+xml; charset=utf-8'})
139 dom = minidom.parseString(response.content.decode())
--> 140 assertion_node = dom.getElementsByTagNameNS("urn:oasis:names:tc:SAML:1.0:assertion", 'Assertion')[0].toxml()
141
142 try:

IndexError: list index out of range

Can someone please help on urgent basis

Microsoft 365 and Office | SharePoint | For business | Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,251 Reputation points
    2022-12-27T07:17:19.727+00:00

    Hi @ASH2212 ,

    According to my research, does the SharePoint account you used have MFA (Multi-Factor Authentication)? If yes, I recommend that you try to connect to SharePoint site using the following code:

     site_url = 'https://contoso.sharepoint.com/'  
     app_principal = {  
         'client_id': '--client-id-goes-here--',  
         'client_secret': '--client-secret-goes-here--',  
     }  
          
     context_auth = AuthenticationContext(url=site_url)  
     context_auth.acquire_token_for_app(client_id=app_principal['client_id'], client_secret=app_principal['client_secret'])  
          
     ctx = ClientContext(site_url, context_auth)  
     web = ctx.web  
     ctx.load(web)  
     ctx.execute_query()  
     print("Web site title: {0}".format(web.properties['Title']))  
    

    In addition, our forum mainly discuss about SharePoint development using C#, as your issue is about Python, we suggest you can create a new thread on techcommunity for help.

    techcommunity :

    https://techcommunity.microsoft.com/t5/sharepoint-developer/bd-p/SharePointDev

    Hope it can help you. Thanks for your understanding.


    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.