cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

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>
""")
 
```
 
2 REPLIES 2

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
Honored Contributor
Honored Contributor

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

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group