How to access company's SharePoint using Python from Linux which is in production (offline environment)?

370 views Asked by At

I am try to establish to my company's SharePoint site from Linux which is in production environment (offline). I also used proxies but failed to establish the connection. I was successful in Windows which is connected to internet. Code:

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from urllib.parse import urlparse, quote, quote_plus


SHAREPOINT_URL = "https://{tenant}.sharepoint.com"
CLIENT_ID = "********"
CLIENT_SECRET = "************"

RELATIVE_URL = "/sites/Path"

proxies = {
    "http": "http://{[email protected]}:quote_plus({password})@{server}:{port}",
    "https": "https://{[email protected]}:quote_plus({password})@{server}:{port}",
}

def __set_proxy(request):
    proxies = proxies
    request.proxies = proxies


def __disable_ssl(request):
    request.verify = False


def _auth() -> ClientContext:
    app = AuthenticationContext(SHAREPOINT_URL)
    app.acquire_token_for_app(CLIENT_ID, CLIENT_SECRET)
    ctx = ClientContext(SHAREPOINT_URL, app)
    ctx.pending_request().beforeExecute += __set_proxy
    ctx.pending_request().beforeExecute += __disable_ssl
    return ctx

if __name__ == "__main__":
    ctx = _auth()

Error I'm facing is:

Traceback (most recent call last):
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/util/connection.py", line 72, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/opt/python_3.9.12/lib/python3.9/socket.py", line 954, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connectionpool.py", line 703, in urlopen
    httplib_response = self._make_request(
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connectionpool.py", line 386, in _make_request
    self._validate_conn(conn)
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connectionpool.py", line 1040, in _validate_conn
    conn.connect()
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connection.py", line 358, in connect
    self.sock = conn = self._new_conn()
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connection.py", line 186, in _new_conn
    raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7f5c54b49970>: Failed to establish a new connection: [Errno -2] Name or service not known

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/test_py39/lib/python3.9/site-packages/requests/adapters.py", line 440, in send
    resp = conn.urlopen(
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/connectionpool.py", line 785, in urlopen
    retries = retries.increment(
  File "/opt/test_py39/lib/python3.9/site-packages/urllib3/util/retry.py", line 592, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='accounts.accesscontrol.windows.net', port=443): Max retries exceeded with url: /63ce7d59-2f3e-42cd-a8cc-be764cff5eb6/tokens/OAuth/2 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f5c54b49970>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/opt/test_py39/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 43, in get_app_only_access_token
    return self._get_app_only_access_token(url_info.hostname, realm)
  File "/opt/test_py39/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 64, in _get_app_only_access_token
    response = requests.post(url=sts_url, headers={'Content-Type': 'application/x-www-form-urlencoded'},
  File "/opt/test_py39/lib/python3.9/site-packages/requests/api.py", line 117, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/opt/test_py39/lib/python3.9/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/opt/test_py39/lib/python3.9/site-packages/requests/sessions.py", line 529, in request
    resp = self.send(prep, **send_kwargs)
  File "/opt/test_py39/lib/python3.9/site-packages/requests/sessions.py", line 645, in send
    r = adapter.send(request, **kwargs)
  File "/opt/test_py39/lib/python3.9/site-packages/requests/adapters.py", line 519, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='accounts.accesscontrol.windows.net', port=443): Max retries exceeded with url: /63ce7d59-2f3e-42cd-a8cc-be764cff5eb6/tokens/OAuth/2 (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f5c54b49970>: Failed to establish a new connection: [Errno -2] Name or service not known'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/projadmin/faker_data/sharepoint_demo/building_sp3.py", line 51, in <module>
    ctx = _auth()
  File "/home/projadmin/faker_data/sharepoint_demo/building_sp3.py", line 21, in _auth
    app.acquire_token_for_app(CLIENT_ID, CLIENT_SECRET)
  File "/opt/test_py39/lib/python3.9/site-packages/office365/runtime/auth/authentication_context.py", line 89, in acquire_token_for_app
    return self._provider.ensure_app_only_access_token()
  File "/opt/test_py39/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 36, in ensure_app_only_access_token
    self._cached_token = self.get_app_only_access_token()
  File "/opt/test_py39/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 45, in get_app_only_access_token
    self.error = e.response.text
AttributeError: 'NoneType' object has no attribute 'text'

More info: Python library I'm using: Office365-REST-Python-Client

When I try this command I'm able to get some response (packets) in Linux: ping {tenant}.sharepoint.com # no https

Failed cases: ping {tenant}.sharepoint.com/site/Path ping https://{tenant}.sharepoint.com

I want to know how to connect to SharePoint site which is in Linux production environment.

0

There are 0 answers