How to connect to a SharePoint site using proxy http request in Python?

Adithya 0 Reputation points
2023-01-11T14:28:53.3033333+00:00

I am able to connect to SharePoint site in online environment using Office 365 Rest Python Client. But for production environment I need to access through http proxy. Can any help how to do this please?

Microsoft 365 and Office SharePoint For business Windows
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. RaytheonXie_MSFT 40,471 Reputation points Microsoft External Staff
    2023-01-12T02:44:33.3833333+00:00

    Hi @Adithya,

    You can connect to sharepoint and set proxy request by following code. Please make a reference

    from office365.runtime.auth.client_credential import ClientCredential
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.webs.web import Web
    from tests import load_settings
    
    settings = load_settings()
    
    
    def set_proxy(request):
        print("Inject proxy settings...")
        #proxies = {settings.get('default', 'site_url'): 'https://127.0.0.1:8888'}
        #request.proxies = proxies
    
    
    ctx = ClientContext(settings.get('default', 'site_url'))\
        .with_credentials(ClientCredential(settings.get('client_credentials', 'client_id'),
                                           settings.get('client_credentials', 'client_secret')))
    
    ctx.pending_request().beforeExecute += set_proxy
    
    # web = ctx.web.get().execute_query()
    # print(web.url)
    
    result = Web.get_context_web_information(ctx).execute_query()
    print(result.value.LibraryVersion)
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.