REST API call throwing 403
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2024 04:21 AM
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
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-30-2024 04:31 AM
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 ?
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-01-2024 10:01 PM
Thanks for sharing your findings,
you can archive the ticket from Options on the right hand side.

