vgrem / Office365-REST-Python-Client

Microsoft 365 & Microsoft Graph Library for Python
MIT License
1.24k stars 325 forks source link

Unable to execute queries from behind a proxy, examples don't work. #609

Open ak9999 opened 1 year ago

ak9999 commented 1 year ago

I am trying to execute a query against SharePoint Online using an app-only context.

Below I am using some code, following the example here: https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/connect_and_set_proxy.py

from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential

def set_proxy(request):
    print("Inject proxy settings...")
    proxies = {'https://example.sharepoint.com/sites/my-site/': 'http://username:password@proxy.example.com:5000'}
    request.proxies = proxies
    request.verify = False

def f():
    ctx = ClientContext('https://example.sharepoint.com/sites/my-site/').with_credentials(
        ClientCredential(client_id=..., client_secret=...)
    )
    ctx.pending_request().beforeExecute += set_proxy
    web = ctx.web.get().execute_query()
    print(web.title)

Instead of getting the title, I end up with the following exceptions in my traceback:

...
ssl.SSLError: [SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1129)
...
During handling of the above exception, another exception occurred: ...
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='accounts.accesscontrol.windows.net', port=443): Max retries exceded with url: /.../tokens/OAuth/2 (Caused by SSLError(SSLError(1, '[SSL: WRONG_VERSION_NUMBER] wrong version number (_ssl.c:1129)')))

During handling of the above exception, another exception occurred: ...
File ...\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'.

I understand that examples were given, but they aren't working for me in my scenario, and it seems to me like the code in set_proxy is never being executed.

ak9999 commented 1 year ago

I ended up setting the following environment variables:

When I set those environment variables, then the code in the set_proxy function runs, but if I use environment variables, then I don't need the function... This isn't making sense to me. Shouldn't set_proxy, as it is written, be running no matter what, and passing the proxies and verify values along with the request?

I want to use the function to set these details because I do not want to use environment variables. Can anyone help me understand?