[Python] How to Interface with SharePoint using Python when your login requires a certificate...

Discussão em 'Python' iniciado por Stack, Agosto 12, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to access a file folder in SharePoint directly within Python so I can see the files stored in there and manipulate them as necessary. However, I don't have a username or password to sign into SharePoint, I use a certificate like from a PIV or CAC card.

    Is there a way to use my certificate to access SharePoint files within a folder in SharePoint using a PIV/CAC to login without Azure?

    Sorry if this isn't thorough enough, new to stack overflow.

    The code below is some I found answering a similar question, of how to interface with SharePoint via Python if you DO have a username and password. Is it possible this could be modified for those using smart cards? If so, please explain how to.

    from office365.runtime.auth.authentication_context import AuthenticationContext
    from office365.sharepoint.client_context import ClientContext
    from office365.sharepoint.files.file import File

    ####inputs########
    # This will be the URL that points to your sharepoint site.
    # Make sure you change only the parts of the link that start with "Your"
    url_shrpt = 'https://YourOrganisation.sharepoint.com/sites/YourSharepointSiteName'
    username_shrpt = 'YourUsername'
    password_shrpt = 'YourPassword'
    folder_url_shrpt = '/sites/YourSharepointSiteName/Shared%20Documents/YourSharepointFolderName/'

    #######################



    ###Authentication###For authenticating into your sharepoint site###
    ctx_auth = AuthenticationContext(url_shrpt)
    if ctx_auth.acquire_token_for_user(username_shrpt, password_shrpt):
    ctx = ClientContext(url_shrpt, ctx_auth)
    web = ctx.web
    ctx.load(web)
    ctx.execute_query()
    print('Authenticated into sharepoint as: ',web.properties['Title'])

    else:
    print(ctx_auth.get_last_error())
    ############################

    Continue reading...

Compartilhe esta Página