Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saml_token_provider file returns an AttributeError - suggestions #888

Open
zxmon21 opened this issue Aug 19, 2024 · 0 comments
Open

saml_token_provider file returns an AttributeError - suggestions #888

zxmon21 opened this issue Aug 19, 2024 · 0 comments

Comments

@zxmon21
Copy link

zxmon21 commented Aug 19, 2024

Hi,
My script creates a CTX frequently. Sometimes there is an (unexplained) error 503 where no valid auth token is returned. That's no big deal - I can accept a failed attempt. I'll try again in 5 min anyway. BUT... office365\runtime\auth\providers\saml_token_provider.py throws an error where it assumes that e.response has a text attribute.
The below error message is slightly misleading because it is based on my modifications to Office365-REST-Python-Client 2.5.11 to isolate the bug. These are the relevant modifications:
image

I'm too amateur a programmer to make changes in the package. Could someone with more programming skills make the required changes so that

  1. either a proper retry within a few seconds is triggered
  2. or the error routine handles errors without response properly
INFO 2024-08-19 04:37:30,463 1724035050.4637942 - Executing query to get CTX
ERROR 2024-08-19 04:37:41,935 1724035061.9352677 - Exception while getting CTX. Got following details:
ERROR 2024-08-19 04:37:41,935 1724035061.9352677 - ("'NoneType' object has no attribute 'text'",)
ERROR 2024-08-19 04:37:41,935 1724035061.9352677 - Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 779, in urlopen
    self._prepare_proxy(conn)
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 1048, in _prepare_proxy
    conn.connect()
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connection.py", line 633, in connect
    self._tunnel()  # type: ignore[attr-defined]
    ^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\http\client.py", line 973, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 503 Service Unavailable

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /sites/myTeams/_api/Web/getFolderByServerRelativeUrl('Shared%20documents%2FGeneral')/Files (Caused by ProxyError('Unable to connect to proxy', OSError('**Tunnel connection failed: 503 Service Unavailable**')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\myPath\method_dictionary.py", line 283, in handle_protocols_from_SharePoint
    for a_file in list_files_by_folder(protocol_ctx,get_path('SharepointDropoffFolder')):
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\myPath\sharepoint.py", line 98, in list_files_by_folder
    ctx.execute_query()
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_runtime_context.py", line 173, in execute_query
    self.pending_request().execute_query(qry)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 37, in execute_query
    response = self.execute_request_direct(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 93, in execute_request_direct
    response = requests.get(
               ^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /sites/myTeams/_api/Web/getFolderByServerRelativeUrl('Shared%20documents%2FGeneral')/Files (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 779, in urlopen
    self._prepare_proxy(conn)
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 1048, in _prepare_proxy
    conn.connect()
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connection.py", line 633, in connect
    self._tunnel()  # type: ignore[attr-defined]
    ^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\http\client.py", line 973, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 503 Service Unavailable

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /_forms/default.aspx?wa=wsignin1.0 (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 112, in get_authentication_cookie
    return self._get_authentication_cookie(token, user_realm.IsFederated)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 287, in _get_authentication_cookie
    headers = {"Content-Type": "application/x-www-form-urlencoded"}
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 637, in post
    return self.request("POST", url, data=data, json=json, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /_forms/default.aspx?wa=wsignin1.0 (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\myPath\sharepoint.py", line 34, in get_sharepoint_context_using_user
    ctx.web.get().execute_query()
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_object.py", line 54, in execute_query
    self.context.execute_query()
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_runtime_context.py", line 173, in execute_query
    self.pending_request().execute_query(qry)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 37, in execute_query
    response = self.execute_request_direct(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 46, in execute_request_direct
    self.beforeExecute.notify(request)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\types\event_handler.py", line 41, in notify
    listener(*args, **kwargs)
  File "C:\Program Files\Python312\Lib\site-packages\office365\sharepoint\client_context.py", line 283, in _authenticate_request
    self.authentication_context.authenticate_request(request)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\authentication_context.py", line 249, in authenticate_request
    self._authenticate(request)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\authentication_context.py", line 206, in _authenticate
    provider.authenticate_request(request)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 85, in authenticate_request
    self.ensure_authentication_cookie()
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 97, in ensure_authentication_cookie
    self._cached_auth_cookies = self.get_authentication_cookie()
                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\auth\providers\saml_token_provider.py", line 114, in get_authentication_cookie
    `self.error = "Error: {}".format(e)`
                 ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'text'

ERROR 2024-08-19 04:37:41,935 1724035061.9352677 - An error occurred getting ClientContext:
ERROR 2024-08-19 04:37:41,935 1724035061.9352677 - AttributeError
ERROR 2024-08-19 04:37:41,950 1724035061.9508836 - Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 779, in urlopen
    self._prepare_proxy(conn)
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 1048, in _prepare_proxy
    conn.connect()
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connection.py", line 633, in connect
    self._tunnel()  # type: ignore[attr-defined]
    ^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\http\client.py", line 973, in _tunnel
    raise OSError(f"Tunnel connection failed: {code} {message.strip()}")
OSError: Tunnel connection failed: 503 Service Unavailable

The above exception was the direct cause of the following exception:

urllib3.exceptions.ProxyError: ('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable'))

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 486, in send
    resp = conn.urlopen(
           ^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\connectionpool.py", line 847, in urlopen
    retries = retries.increment(
              ^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\urllib3\util\retry.py", line 515, in increment
    raise MaxRetryError(_pool, url, reason) from reason  # type: ignore[arg-type]
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /sites/myTeams/_api/Web/getFolderByServerRelativeUrl('Shared%20documents%2FGeneral')/Files (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\myPath\method_dictionary.py", line 283, in handle_protocols_from_SharePoint
    for a_file in list_files_by_folder(protocol_ctx,get_path('SharepointDropoffFolder')):
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\myPath\sharepoint.py", line 98, in list_files_by_folder
    ctx.execute_query()
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_runtime_context.py", line 173, in execute_query
    self.pending_request().execute_query(qry)
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 37, in execute_query
    response = self.execute_request_direct(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\office365\runtime\client_request.py", line 93, in execute_request_direct
    response = requests.get(
               ^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\api.py", line 73, in get
    return request("get", url, params=params, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\api.py", line 59, in request
    return session.request(method=method, url=url, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 589, in request
    resp = self.send(prep, **send_kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\sessions.py", line 703, in send
    r = adapter.send(request, **kwargs)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Program Files\Python312\Lib\site-packages\requests\adapters.py", line 513, in send
    raise ProxyError(e, request=request)
requests.exceptions.ProxyError: HTTPSConnectionPool(host='mysite.sharepoint.com', port=443): Max retries exceeded with url: /sites/myTeams/_api/Web/getFolderByServerRelativeUrl('Shared%20documents%2FGeneral')/Files (Caused by ProxyError('Unable to connect to proxy', OSError('Tunnel connection failed: 503 Service Unavailable')))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\myPath\method_dictionary.py", line 293, in handle_protocols_from_SharePoint
    logger_m.info('New context is valid from {}'.format(protocol_ctx.context_info._valid_from))
                                                        ^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'bool' object has no attribute 'context_info'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants