function test_drive_items
Tests access to items in a Microsoft SharePoint drive by querying the Microsoft Graph API and verifying the response.
/tf/active/vicechatdev/SPFCsync/check_tenant_config.py
143 - 171
simple
Purpose
This function validates that a given access token has proper permissions to access and list items in a specific SharePoint drive. It makes an authenticated request to the Microsoft Graph API to retrieve children items from the drive's root folder, providing feedback on success or failure. This is typically used for testing authentication, permissions, and API connectivity in SharePoint integration scenarios.
Source Code
def test_drive_items(access_token, site_id, drive_id):
"""Test accessing items in the drive."""
headers = {
'Authorization': f'Bearer {access_token}',
'Accept': 'application/json'
}
try:
# Get items in the drive root
items_url = f"https://graph.microsoft.com/v1.0/sites/{site_id}/drives/{drive_id}/root/children"
response = requests.get(items_url, headers=headers)
if response.status_code == 200:
print("✅ Drive items accessible via Graph API")
items_data = response.json()
if 'value' in items_data:
print(f" Found {len(items_data['value'])} items in root")
return True
else:
print(" ⚠️ No items found")
return True # Still successful access
else:
print(f"❌ Drive items API failed: {response.status_code}")
return False
except Exception as e:
print(f"❌ Exception testing drive items: {e}")
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
access_token |
- | - | positional_or_keyword |
site_id |
- | - | positional_or_keyword |
drive_id |
- | - | positional_or_keyword |
Parameter Details
access_token: A valid OAuth 2.0 bearer token with appropriate Microsoft Graph API permissions (e.g., Files.Read, Sites.Read.All) to access SharePoint resources. This token is used for authentication in the Authorization header.
site_id: The unique identifier (GUID) of the SharePoint site containing the drive. This can be obtained from the Microsoft Graph API sites endpoint or SharePoint site settings.
drive_id: The unique identifier (GUID) of the specific drive within the SharePoint site. Each SharePoint site can have multiple drives (document libraries), and this ID specifies which one to query.
Return Value
Returns a boolean value: True if the API request succeeds (status code 200) regardless of whether items are found, or False if the request fails (non-200 status code) or an exception occurs. The function also prints status messages to stdout indicating success, warnings, or errors.
Dependencies
requests
Required Imports
import requests
Usage Example
import requests
# Obtain access token through OAuth flow (example simplified)
access_token = "eyJ0eXAiOiJKV1QiLCJhbGc..."
site_id = "contoso.sharepoint.com,12345678-1234-1234-1234-123456789012,87654321-4321-4321-4321-210987654321"
drive_id = "b!abcdefgh12345678..."
# Test drive access
result = test_drive_items(access_token, site_id, drive_id)
if result:
print("Drive access test passed")
else:
print("Drive access test failed")
Best Practices
- Ensure the access token is fresh and not expired before calling this function
- Handle the boolean return value appropriately in production code rather than relying solely on print statements
- Consider implementing retry logic for transient network failures
- Validate that the access token has the minimum required permissions (Files.Read or Sites.Read.All) before making the API call
- Be aware that this function prints directly to stdout; consider refactoring to use logging for production environments
- The function returns True even when no items are found (empty drive), which may need different handling depending on use case
- Consider adding timeout parameters to the requests.get() call to prevent hanging on slow connections
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function test_site_drive_access 89.1% similar
-
function test_graph_sites_api 75.9% similar
-
function test_graph_api_access 75.4% similar
-
function test_sharepoint_api_call 75.2% similar
-
function test_sharepoint_with_token 74.0% similar