Search code examples
azurepowershelldatabricksazure-databricks

Unable to list the files using RestAPI in Databricks


I am admin of databricks workspace, created PAT token to make API call but continue to get unauthorized access error while trying to check if a folder exist or not using the below code

$token = 'MYPAT'
$folderCheckUrl = "$workspaceUrl/api/2.0/dbfs/list?path=$RemoteFolderPath"
$folderListResponse = Invoke-RestMethod -Uri $folderCheckUrl -Headers @{Authorization = "Bearer $token" } -Method GET

Any suggestion what's going wrong here?


Solution

  • In general you will receive Unauthorized access error when right credentials or enough permissions are not provided for the token. I have used the same script as yours and could able to retrieve the desired results after directly generating my token from databrick's User Settings >> Access tokens >> Generate new token >> Generate.

    enter image description here

    Make sure you are pointing to the right folder. Below is the complete script that I worked for me to retrieve files from databricks.

    enter image description here

    $workspaceUrl = '<WORKSPACE_URL>'
    $RemoteFolderPath = 'dbfs:/FileStore/tables'
    $token = '<GENERATED_TOKEN>'
    $folderCheckUrl = "$workspaceUrl/api/2.0/dbfs/list?path=$RemoteFolderPath"
    $folderListResponse = Invoke-RestMethod -Uri $folderCheckUrl -Headers @{Authorization = "Bearer $token" } -Method GET
    $folderListResponse
    

    Results:

    enter image description here

    Note: I have contributor role for my databricks workspace