Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Chilkat2-Python Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
JavaScript
MHT / HTML Email
MIME
Markdown
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
Regular Expressions
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Chilkat2-Python) SharePoint Get Site ID

See more SharePoint Examples
This example demonstrates how to use the Chilkat HttpCurl class together with Microsoft Graph and OAuth2 Client Credentials authentication to retrieve the ID of a SharePoint site. The example automatically obtains an access token, sends a request to Microsoft Graph to locate a site by hostname and site name, and extracts the site's ID, description, and hostname from the JSON response. The retrieved site ID can then be used in subsequent SharePoint operations such as accessing document libraries, browsing folders, and managing files.

Note: This example requires Chilkat v11.5.0 or greater.

Chilkat2 Python Downloads

install with pip

pip3 install chilkat2

or download... Python Module for Windows, Linux, Alpine Linux, MacOS

import sys
import chilkat2

success = False

# This example retrieves the Microsoft Graph site ID for a SharePoint site.
# 
# The site ID is required for many subsequent Microsoft Graph operations,
# such as enumerating document libraries, listing files, downloading content,
# or creating folders within a SharePoint site.

success = False

# --------------------------------------------------------------------------------------------------------
# Before running this example, create an Azure App Registration and grant it
# the Microsoft Graph permissions required to access SharePoint.
# 
# The application will authenticate using OAuth2 Client Credentials.
# See:
# How to Create SharePoint App Registration for OAuth 2.0 Client Credentials
# --------------------------------------------------------------------------------------------------------

# Build a JSON authentication configuration.
# HttpCurl will use this information to automatically obtain OAuth2 access tokens.
jsonAuth = chilkat2.JsonObject()

# Enable secret lookup.
# 
# Instead of hard-coding sensitive values such as the client ID,
# client secret, and token endpoint, secret specification strings
# are used.  Chilkat automatically retrieves the actual values from
# Windows Credential Manager (Windows) or Apple Keychain (macOS).
# 
# See:
# Secret Specification Strings
jsonAuth.EnableSecrets = True

success = jsonAuth.UpdateString("oauth2.client_id","!!sharepoint|oauth2|client_id")
if (success == True):
    success = jsonAuth.UpdateString("oauth2.client_secret","!!sharepoint|oauth2|client_secret")

if (success == True):
    success = jsonAuth.UpdateString("oauth2.token_endpoint","!!sharepoint|oauth2|token_endpoint")

if (success == False):
    print(jsonAuth.LastErrorText)
    sys.exit()

# Request Microsoft Graph permissions that were granted to the application.
jsonAuth.UpdateString("oauth2.scope","https://graph.microsoft.com/.default")

# ---------------------------------------------------------------------------------------------------

curl = chilkat2.HttpCurl()

# Associate the OAuth2 configuration with HttpCurl.
# 
# When the request is executed, Chilkat automatically obtains an access token
# (if needed) and adds the Authorization: Bearer header to the HTTP request.
curl.SetAuth(jsonAuth)

# Define variables whose values are already known.
# 
# These variables are referenced in the curl command using
# {{variable_name}} substitution syntax.
curl.SetVar("sharepoint_hostname","example.sharepoint.com")
curl.SetVar("site_name","test")

# Define the values we want to extract from the JSON response.
# 
# The first argument is a JSON path within the response.
# The second argument is the name of the HttpCurl variable that
# will receive the extracted value.
curl.AddTargetOutput("id","site_id")
curl.AddTargetOutput("description","site_description")
curl.AddTargetOutput("siteCollection.hostname","site_hostname")

# Microsoft Graph request:
# 
# GET /sites/{hostname}:/sites/{site-name}
# 
# This returns information about the SharePoint site, including
# the site ID that is needed for many later SharePoint operations.
# 
# No Authorization header is included because HttpCurl automatically
# adds it when OAuth2 authentication is configured.
curlCommand = "curl -X GET \"https://graph.microsoft.com/v1.0/sites/{{sharepoint_hostname}}:/sites/{{site_name}}\""

# Execute the request.
success = curl.DoYourThing(curlCommand)
if (success == False):
    print(curl.LastErrorText)
    sys.exit()

# A successful Graph response should return HTTP 200.
# Any other status code typically indicates an authentication,
# permission, or resource lookup error.
statusCode = curl.StatusCode
if (statusCode != 200):
    print(curl.ResponseBodyStr)
    print("status code = " + str(statusCode))
    sys.exit()

# Verify that all target outputs were successfully extracted.
# 
# The special variable name "!" tells VarDefined to return True
# only if every target output defined by AddTargetOutput was found
# in the response JSON.
if (curl.VarDefined("!") == False):
    print(curl.ResponseBodyStr)
    print("Not all target outputs were found...")
    sys.exit()

# Display the extracted values.
print("site_id = " + curl.GetVar("site_id"))
print("site_description = " + curl.GetVar("site_description"))
print("site_hostname = " + curl.GetVar("site_hostname"))

print("Success.")

 

© 2000-2026 Chilkat Software, Inc. All Rights Reserved.