vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.35k stars 336 forks source link

Connecting to Sharepoint #334

Open ahulist opened 3 years ago

ahulist commented 3 years ago

I would like to CRUD files on Sharepoint. To do this I'm trying to follow this basic example:

from office365.sharepoint.client_context import ClientContext

settings = {
    'url': 'https://*****.sharepoint.com/sites/*****',
    'user_credentials': {
        'username': '*****@gmail.com',
        'password': '*****',
    },
}

ctx = ClientContext(settings["url"]).with_user_credentials(settings.get('user_credentials').get('username'),
                                                           settings.get('user_credentials').get('password'))

web = ctx.web.get().execute_query()
print(web.properties["Url"])

which gives me an Error:

IndexError                                Traceback (most recent call last)
<ipython-input-23-c5907526ff22> in <module>
     12                                                            settings.get('user_credentials').get('password'))
     13 
---> 14 web = ctx.web.get().execute_query()
     15 print(web.properties["Url"])

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_object.py in execute_query(self)
     31 
     32     def execute_query(self):
---> 33         self.context.execute_query()
     34         return self
     35 

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_runtime_context.py in execute_query(self)
    136     def execute_query(self):
    137         if self.has_pending_request:
--> 138             self.pending_request().execute_query()
    139 
    140     def add_query(self, query, to_begin=False):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_request.py in execute_query(self)
     72                 request = self.build_request()
     73                 self.beforeExecute.notify(request)
---> 74                 response = self.execute_request_direct(request)
     75                 response.raise_for_status()
     76                 self.process_response(response)

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\odata\odata_request.py in execute_request_direct(self, request)
     32     def execute_request_direct(self, request):
     33         self.ensure_media_type(request)
---> 34         return super(ODataRequest, self).execute_request_direct(request)
     35 
     36     def build_request(self):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\client_request.py in execute_request_direct(self, request_options)
     84         :type request_options: office365.runtime.http.request_options.RequestOptions
     85         """
---> 86         self.context.authenticate_request(request_options)
     87         if request_options.method == HttpMethod.Post:
     88             if request_options.is_bytes or request_options.is_file:

D:\Anaconda\envs\hplc\lib\site-packages\office365\sharepoint\client_context.py in authenticate_request(self, request)
    151 
    152     def authenticate_request(self, request):
--> 153         self._auth_context.authenticate_request(request)
    154 
    155     def _build_modification_query(self, request):

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\authentication_context.py in authenticate_request(self, request)
     82         """Authenticate request
     83         :type request: RequestOptions"""
---> 84         self._provider.authenticate_request(request)

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in authenticate_request(self, request)
     71         """
     72         logger = self.logger(self.authenticate_request.__name__)
---> 73         self.ensure_authentication_cookie()
     74         logger.debug_secrets(self._cached_auth_cookies)
     75         cookie_header_value = "; ".join(["=".join([key, str(val)]) for key, val in self._cached_auth_cookies.items()])

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in ensure_authentication_cookie(self)
     78     def ensure_authentication_cookie(self):
     79         if self._cached_auth_cookies is None:
---> 80             self._cached_auth_cookies = self.get_authentication_cookie()
     81         return True
     82 

D:\Anaconda\envs\hplc\lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py in get_authentication_cookie(self)
     91             user_realm = self._get_user_realm()
     92             if user_realm.IsFederated:
---> 93                 token = self._acquire_service_token_from_adfs(user_realm.STSAuthUrl)
     94             else:
     95                 token = self._acquire_service_token()

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

IndexError: list index out of range

Any ideas? It doesn't feel like an authentication problem.

nsmcan commented 3 years ago

@ahulist Logging the response.content.decode() from line 135 of saml_token_provider.py, and reading it might help you to understand, why Assertion was not provided

nsmcan commented 3 years ago

@vgrem, though you guys got this code with the minidom module from my suggestion, I ended up using the simple regex match there. It must be the most effective, and don't require minidom, which you don't use anywhere else:

            match = re.search(r'<saml:Assertion.+</saml:Assertion>', response.content.decode())
            if match is None:
                self.error = 'Cannot get security assertion for user {0} from {1}'.format(self.__username, adfs_url)
                logger.error(self.error)
                return None
            assertion_node = match.group()

This chunk is from an older 2.2.1.1 version

vgrem commented 3 years ago

@nsmcan, i believe we did #297 :)

vgrem commented 3 years ago

@nsmcan, although this change seems unfortunately impacted to broken auth (for federated signin with ADFS), related issues reported here:

nsmcan commented 3 years ago

@vgrem, we need to double-check code against a reference implementation found on Microsoft. I will ask our SharePoint admin (@Zerg00s) to help me installing and configuring a couple of test instances, where it could be properly tested

AkechiShiro commented 1 year ago

Hey @nsmcan, @vgrem @Zerg00s, was this issue abandoned/forgotten ? has this been solved in any way yet (I believe it is not fixed at the moment) or considered a won't fix issue ? Is there any temporary workaround for this issue ? Reverting back to an old commit ?

chelsy-mena commented 4 months ago

+1 to @AkechiShiro 's questions. Although it was working up to a few days ago, it seems something broke it, maybe on my company's end; but I'm struggling to find a solution... should I update or downgrade?

qw-dataninja commented 2 months ago

@chelsy-mena did you find a solution to this?