SmartSheet API to access summary report using python

I am using this code to access the summary reports but am getting this error. Any help would be great thank you!

Best Answer

  • jmyzk_cloudsmart_jp
    jmyzk_cloudsmart_jp ✭✭✭✭✭✭
    Answer ✓

    Hi @Wildcat2024

    I usually use Smartsheet Python SDK, but since it does not have the Update Report method, I used requests to like your code.

    Test code

    import requests
    import json api_token = 'YOUR_SMARTSHEET_API_TOKEN' report_id = 'YOUR_REPORT_ID' #Smartsheet API endpoint to get the report details, including the required query parameter get_url = f'https://api.smartsheet.com/2.0/reports/{report_id}?level=2' Headers for the request headers = {
    'Authorization': f'Bearer {api_token}',
    'Content-Type': 'application/json'
    } #Make the GET request to retrieve the report details response = requests.get(get_url, headers=headers) #Check the response status if response.status_code == 200:
    report = response.json()
    current_report_name = report.get('name', 'Unnamed Report')
    print(f'Current Report Name: {current_report_name}')
    # Modify the report name
    new_report_name = f'new {current_report_name}'

    # Smartsheet API endpoint to update the report
    update_url = f'https://api.smartsheet.com/2.0/reports/{report_id}?level=2'

    # Data payload for the request
    data = {
    'name': new_report_name
    }

    # Make the PUT request to update the report name
    update_response = requests.put(update_url, headers=headers, data=json.dumps(data))

    # Check the response
    if update_response.status_code == 200:
    print('Report name updated successfully')
    else:
    print('Failed to update report name')
    print('Status Code:', update_response.status_code)
    print('Response:', update_response.text) else:
    print('Failed to retrieve report details')
    print('Status Code:', response.status_code)
    print('Response:', response.text)

    Output

    Current Report Name: new Sheet Summary Report
    Failed to update report name
    Status Code: 400
    Response: {
    "errorCode" : 5623,
    "message" : "The report you are attempting to open is a sheet summary report, which requires a query parameter of 'level=2' (or higher if available) to be specified in the API request.",
    "refId" : "urid6g"
    }

    Although the requests give me an error code, the code does update the report's name.

    I added 'level=2' to the put request's URL, but I still got the error message, though the update was working.

Answers

  • jmyzk_cloudsmart_jp
    jmyzk_cloudsmart_jp ✭✭✭✭✭✭
    Answer ✓

    Hi @Wildcat2024

    I usually use Smartsheet Python SDK, but since it does not have the Update Report method, I used requests to like your code.

    Test code

    import requests
    import json api_token = 'YOUR_SMARTSHEET_API_TOKEN' report_id = 'YOUR_REPORT_ID' #Smartsheet API endpoint to get the report details, including the required query parameter get_url = f'https://api.smartsheet.com/2.0/reports/{report_id}?level=2' Headers for the request headers = {
    'Authorization': f'Bearer {api_token}',
    'Content-Type': 'application/json'
    } #Make the GET request to retrieve the report details response = requests.get(get_url, headers=headers) #Check the response status if response.status_code == 200:
    report = response.json()
    current_report_name = report.get('name', 'Unnamed Report')
    print(f'Current Report Name: {current_report_name}')
    # Modify the report name
    new_report_name = f'new {current_report_name}'

    # Smartsheet API endpoint to update the report
    update_url = f'https://api.smartsheet.com/2.0/reports/{report_id}?level=2'

    # Data payload for the request
    data = {
    'name': new_report_name
    }

    # Make the PUT request to update the report name
    update_response = requests.put(update_url, headers=headers, data=json.dumps(data))

    # Check the response
    if update_response.status_code == 200:
    print('Report name updated successfully')
    else:
    print('Failed to update report name')
    print('Status Code:', update_response.status_code)
    print('Response:', update_response.text) else:
    print('Failed to retrieve report details')
    print('Status Code:', response.status_code)
    print('Response:', response.text)

    Output

    Current Report Name: new Sheet Summary Report
    Failed to update report name
    Status Code: 400
    Response: {
    "errorCode" : 5623,
    "message" : "The report you are attempting to open is a sheet summary report, which requires a query parameter of 'level=2' (or higher if available) to be specified in the API request.",
    "refId" : "urid6g"
    }

    Although the requests give me an error code, the code does update the report's name.

    I added 'level=2' to the put request's URL, but I still got the error message, though the update was working.