如何通过API修改Smartsheet中已发布项目的访问控制(Public/Organization)权限
Here's a practical, step-by-step guide to updating the access control (Public/Organization) for your published Smartsheet items via the API:
Prerequisites
- Your Smartsheet API token (generate this from your account settings under "API Access").
- Your downloaded list of published items, which must include each item's
idandtype(sheet, dashboard, report, etc.). - Ensure your API token is linked to a user with Admin or Publisher permissions—these roles are required to modify published settings.
Core API Basics
Smartsheet uses type-specific endpoints for published items. You'll send a PUT request with a JSON payload to set the accessLevel (either PUBLIC or ORGANIZATION). You also need to include readOnlyEnabled (set to true for standard read-only published access; use false only if you want the published item to be editable).
Step-by-Step Implementation
1. Match Item Types to API Endpoints
Use these endpoints based on your published item's type:
- Sheets:
PUT /sheets/{sheetId}/publish - Dashboards:
PUT /dashboards/{dashboardId}/publish - Reports:
PUT /reports/{reportId}/publish
2. Request Payload Examples
To set access to Organization:
{ "readOnlyEnabled": true, "accessLevel": "ORGANIZATION" }
To set access to Public:
{ "readOnlyEnabled": true, "accessLevel": "PUBLIC" }
3. Sample Python Script
This script loops through your published items list and updates each one to your desired access level:
import requests import json # Replace with your actual API token SMARTSHEET_API_TOKEN = "your_api_token_here" BASE_API_URL = "https://api.smartsheet.com/2.0" # Replace with your downloaded published items data published_items = [ {"id": 123456, "type": "sheet"}, {"id": 789012, "type": "dashboard"}, {"id": 345678, "type": "report"} ] # Set your target access level: "PUBLIC" or "ORGANIZATION" target_access_level = "ORGANIZATION" for item in published_items: item_id = item["id"] item_type = item["type"] # Select the correct API endpoint if item_type == "sheet": endpoint = f"{BASE_API_URL}/sheets/{item_id}/publish" elif item_type == "dashboard": endpoint = f"{BASE_API_URL}/dashboards/{item_id}/publish" elif item_type == "report": endpoint = f"{BASE_API_URL}/reports/{item_id}/publish" else: print(f"Skipping unknown item type: {item_type} (ID: {item_id})") continue # Prepare request headers and payload headers = { "Authorization": f"Bearer {SMARTSHEET_API_TOKEN}", "Content-Type": "application/json" } payload = { "readOnlyEnabled": True, "accessLevel": target_access_level } # Send the update request response = requests.put(endpoint, data=json.dumps(payload), headers=headers) # Handle the response if response.status_code == 200: print(f"Successfully updated {item_type} {item_id} to {target_access_level}") else: print(f"Failed to update {item_type} {item_id}. Status: {response.status_code}, Error: {response.text}")
Key Notes & Troubleshooting
- Rate Limits: Smartsheet enforces 600 requests per minute for most plans. If you get a
429status code, retry after the time specified in theRetry-Afterresponse header. - Permission Errors: A
403 Forbiddenresponse means your API user lacks the necessary permissions—double-check their role in Smartsheet. - Verify Changes: To confirm updates, send a
GETrequest to the same endpoint (e.g.,GET /sheets/{sheetId}/publish) and check theaccessLevelfield in the response.
内容的提问来源于stack exchange,提问作者jeeva sr

