Skip to content

Best way to refresh token when using cert flow #1013

@ckantzer

Description

@ckantzer

Hello,
We are using the certificate flow to authenticate with Sharepoint and create a ctx object to run queries. Once we create the ctx object the script needs to run thousands of queries, which can take over an hour to complete. After an hour the Sharepoint token expires and we get 401 errors on any subsequent api requests.

What is the best way to refresh the token? Should we get a new ctx object for each execute_query()? Or is there a another way to refresh the token with the existing ctx?

Sample auth code:

site_url = "https://www.sharepoint.com/sites/oursite
cert_settings = {
   'tenant': 'yyyy',
   'client_id': 'cccc',
   'thumbprint': "tttt",
   'cert_path': 'sharepoint.pem'
}

ctx = ClientContext(site_url).with_client_certificate(**cert_settings)

The script will successfully execute queries for an hour, but after an hour the token expires and we get the following exception:
401 Client Error: Unauthorized for url: https://www.sharepoint.com/sites/oursite

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions