This SAS KB article describes a structured approach to creating an inventory of SAS Viya content stored in SAS Content folders using Python and the Folders REST API. This article outlines how to recursively traverse folder structures; identify non-folder content, such as reports, decisions, and models; and export the results into a structured CSV file for further analysis.
The approach builds upon a SAS Communities example by adapting it for execution within SAS® Studio and enhancing it with capabilities required for real-world use. These enhancements include support for internal authentication using the SAS Viya session token, external execution using OAuth authentication, pagination handling for large folders, improved error handling, and metadata enrichment.
In addition, this article highlights important considerations related to API-level authorization, including scenarios where content is accessible through the user interface but is restricted when accessed via the API.
This article is intended to help SAS administrators and technical users implement a reliable and repeatable method for content inventory, supporting activities such as migration planning, governance, and troubleshooting.
Use this version when you run Python inside SAS Studio on the same SAS Viya environment. This version obtains the active SAS Viya compute session token from SAS_SERVICES_TOKEN.
Before you run the code, do the following:
- Update viya_url to the base URL of the SAS Viya environment. Do not include /SASStudio, /SASDrive, or /SASVisualAnalytics.
- Update startFolderPath to the target folder URI. The expected format is /folders/folders/.
- Test on a small folder first before scanning a broad content hierarchy.
Use this version when running from a local machine, server, automation host, or external Python runtime. In this mode, SAS Studio does not provide SAS_SERVICES_TOKEN. You must supply a valid OAuth access token.
External Execution Notes:
- Confirm that the Python host can reach the SAS Viya base URL.
- Obtain an OAuth access token using an approved method for the environment.
- Do not hardcode secrets in production scripts.
- Use a trusted certificate bundle instead of verify=False whenever possible.
Troubleshooting
401 Full authentication is required
- Cause: A missing, expired, or invalid token, or the token is not passed in the Authorization header.
- Internal Execution: Confirm that SAS_SERVICES_TOKEN exists.
- External Execution: Obtain a valid OAuth token and pass it as Bearer.
SAS_SERVICES_TOKEN was not found
- Cause: The script is not running inside SAS Studio or SAS Viya compute.
- Resolution: Run the internal version from SAS Studio, or use the external version with OAuth.
Certificate Warning
- Cause: The examples use verify=False for testing simplicity.
- Resolution: For production, use a trusted certificate bundle, such as verify='/path/to/certificate-bundle.pem'.
CSV was created, but no rows are present
- The starting folder might contain only subfolders and no non-folder content.
- The user might not have permission to see the content.
- The wrong folder URI might have been supplied.
- Test with a known folder and check memberCount in the folder resource response.
Notes and Considerations
- This script inventories content that appears under SAS Content folders.
- This script does not export, copy, modify, or delete content.
- This script performs only GET requests.
- Permissions affect what the API returns.
- Administrator access can provide broader visibility, but authorization rules still matter.
- For production use, configure certificate verification rather than using verify=False.
- For large environments, test on a smaller folder first before scanning a broad hierarchy.