REST API call throwing 403

Mehdi-LAMRANI
New Contributor II
Hi,
 
Calling curl works just fine from my workspace
 
````
%sh
curl --request GET "https://${DATABRICKS_HOST}/api/2.0/clusters/list" --header "Authorization: Bearer ${DATABRICKS_API_TOKEN}"
```
But when I transpose the same logic to a script in javascript (in the same notebook) it throws a 403 error 
I am on Azure. 
What am I missing ?
 
```
displayHTML(f"""
<!DOCTYPE html>
<html>
<body>

<textarea id="log" rows="10" cols="50" readonly></textarea>

<script>
const DATABRICKS_HOST = "{os.environ['DATABRICKS_HOST']}";
const DATABRICKS_API_TOKEN = "{os.environ['DATABRICKS_API_TOKEN']}";

async function loadClusterData() {{
try {{
const response = await fetch(`https://${{DATABRICKS_HOST}}/api/2.0/clusters/list`, {{
method: 'GET',
headers: {{
'Authorization': `Bearer ${{DATABRICKS_API_TOKEN}}`,
'Content-Type': 'application/json'
}},
}});

document.getElementById('log').value = "Response status: " + response.status;

if (!response.ok) {{
throw new Error(`HTTP error! status: ${{response.status}}`);
}}

const data = await response.json();
document.getElementById('log').value += "\\nResponse data: " + JSON.stringify(data);
}} catch (error) {{
document.getElementById('log').value += "\\nFailed to load cluster data: " + error.message;
}}
}}

window.onload = loadClusterData;
</script>

</body>
</html>
""")
 
```
 

Mehdi-LAMRANI
New Contributor II

Never Mind, I was using 

dbutils.notebook.entry_point.getDbutils().notebook().getContext().apiToken().get()
to get the api token and for some reason I can't figure out, that token does not work in the second case.
I had to use one of my generated tokens. 
Go figure..
 
PS ; Isn't there any way to delete a question ?

 

NandiniN
Databricks Employee
Databricks Employee

Thanks for sharing your findings,

you can archive the ticket from Options on the right hand side.

 Screenshot 2024-06-02 at 10.30.22.png