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

Can't run script behind proxy #896

Open
fsimoes047 opened this issue Sep 27, 2024 · 1 comment
Open

Can't run script behind proxy #896

fsimoes047 opened this issue Sep 27, 2024 · 1 comment

Comments

@fsimoes047
Copy link

fsimoes047 commented Sep 27, 2024

Hi,
trying to run this simple script:

import sys
import os
import requests
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

from office365.runtime.auth.client_credential import ClientCredential
from office365.runtime.http.request_options import RequestOptions


baseurl = sys.argv[1]
basesite = sys.argv[2]
siteurl = baseurl + basesite

remotepath = sys.argv[3]

file_name = sys.argv[4]
file_content = sys.argv[5]
client_id_site = os.getenv('client_id')
client_secret_site = os.getenv('client_secret')


def set_proxy(request):
    proxies = {siteurl:'http://xxx.xxx.90.15:443/' }
    request.proxies = proxies
    

def disable_ssl(request):
    request.verify = False  # Disable certification verification


def sh_upload():

    pasta,name = os.path.split(remotepath)

    ctx_auth = AuthenticationContext(siteurl) # should also be the siteurl
    ctx_auth.acquire_token_for_app(client_id_site, client_secret_site)
    ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.

    
    ctx.pending_request().beforeExecute += set_proxy ##
    ctx.pending_request().beforeExecute += disable_ssl ##

    file = ctx.web.get_folder_by_server_relative_url(pasta).upload_file(file_name, file_content).execute_query()
    return file

if __name__ == '__main__':
    res = sh_upload()
    print(res)

Runs fine on a network without proxy, but in the internal network behind a proxy fail with this message:

Traceback (most recent call last):
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
conn = connection.create_connection(
File "/opt/tkapp/env_airflow/lib64/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 "/usr/lib64/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/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connectionpool.py", line 716, in urlopen
httplib_response = self._make_request(
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connectionpool.py", line 404, in _make_request
self._validate_conn(conn)
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connectionpool.py", line 1061, in _validate_conn
conn.connect()
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connection.py", line 363, in connect
self.sock = conn = self._new_conn()
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connection.py", line 186, in _new_conn
raise NewConnectionError(
urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPSConnection object at 0x7fd8ee90d3d0>: 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/tkapp/env_airflow/lib64/python3.9/site-packages/requests/adapters.py", line 667, in send
resp = conn.urlopen(
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/connectionpool.py", line 802, in urlopen
retries = retries.increment(
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/urllib3/util/retry.py", line 594, in increment
raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='kyndrylde.sharepoint.com', port=443): Max retries exceeded with url: /teams/PortugalPHC (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fd8ee90d3d0>: 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/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 45, in get_app_only_access_token
realm = self._get_realm_from_target_url()
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 88, in _get_realm_from_target_url
response = requests.head(url=self.url, headers={"Authorization": "Bearer"})
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/requests/api.py", line 100, in head
return request("head", url, **kwargs)
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/requests/api.py", line 59, in request
return session.request(method=method, url=url, **kwargs)
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
File "/opt/tkapp/env_airflow/lib64/python3.9/site-packages/requests/adapters.py", line 700, in send
raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='kyndrylde.sharepoint.com', port=443): Max retries exceeded with url: /teams/PortugalPHC (Caused by NewConnectionError('<urllib3.connection.HTTPSConnection object at 0x7fd8ee90d3d0>: 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/tkapp/sharepoint/sharepoint_upload_file.py", line 61, in
res = sh_upload()
File "/opt/tkapp/sharepoint/sharepoint_upload_file.py", line 57, in sh_upload
file = ctx.web.get_folder_by_server_relative_url(pasta).upload_file(file_name, file_content).execute_query()
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/client_object.py", line 54, in execute_query
self.context.execute_query()
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/client_runtime_context.py", line 173, in execute_query
self.pending_request().execute_query(qry)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/client_request.py", line 37, in execute_query
response = self.execute_request_direct(request)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/client_request.py", line 46, in execute_request_direct
self.beforeExecute.notify(request)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/types/event_handler.py", line 41, in notify
listener(*args, **kwargs)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/sharepoint/client_context.py", line 284, in _authenticate_request
self.authentication_context.authenticate_request(request)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/authentication_context.py", line 249, in authenticate_request
self._authenticate(request)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/authentication_context.py", line 239, in _authenticate
provider.authenticate_request(request)
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 34, in authenticate_request
self.ensure_app_only_access_token()
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 39, in ensure_app_only_access_token
self._cached_token = self.get_app_only_access_token()
File "/home/ttauto/.local/lib/python3.9/site-packages/office365/runtime/auth/providers/acs_token_provider.py", line 54, in get_app_only_access_token
raise ValueError(self.error)
ValueError: Acquire app-only access token failed.

Is this a bug or something wrong with the script?
Version of Office_REST_Python_Client==2.5.12
Thank you
Fernando

@danieldean
Copy link

Hi,

I'm not sure if this will solve your issue for now but I'm also behind a proxy and got this to work with:

# Overriding proxy environment variables as the library does not provide a way to do this.
os.environ['HTTP_PROXY'] = os.environ['http_proxy'] = '<proxy>'
os.environ['HTTPS_PROXY'] = os.environ['https_proxy'] = '<proxy>'

Before attempting to connect. Maybe this is of some help.

Daniel

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

3 participants