Connect to SharePoint using Azure AD app-only with a certificate (PEM file).¶
Loads the certificate private key from a PEM file, optionally passphrase-protected.
Prerequisites: - An app registered in Azure AD with a certificate credential - Upload the certificate public key to the app registration - Grant the app appropriate SharePoint permissions
Reference¶
import argparse
from office365.sharepoint.client_context import ClientContext
from tests.settings import cert_path, cert_thumbprint, client_id, site_url, tenant
def main():
parser = argparse.ArgumentParser(description="Connect to SharePoint app-only with a certificate (PEM file)")
parser.add_argument("--thumbprint", default=cert_thumbprint, help="certificate thumbprint")
parser.add_argument("--cert-path", default=cert_path, help="path to the PEM private key file")
parser.add_argument("--passphrase", default=None, help="private key passphrase (if the key is encrypted)")
args = parser.parse_args()
ctx = ClientContext(site_url).with_client_certificate(
tenant=tenant,
client_id=client_id,
thumbprint=args.thumbprint,
cert_path=args.cert_path,
passphrase=args.passphrase,
)
web = ctx.web.get().execute_query()
print(web.title)
if __name__ == "__main__":
main()