GitHub user dosubot[bot] added a comment to the discussion: Deck.gl map change
style directly on dashboard
<!-- Answer -->
To add a CSRF token to the Python function for calling the
`/api/v1/dashboard/import/` API, you need to include the `X-CSRFToken` header
in the request. Here's how you can modify the function to include the CSRF
token:
```python
import requests
import json
from io import BytesIO
def import_dashboard(api_url, dashboard_export, auth_token, csrf_token,
passwords=None, overwrite=False, ssh_tunnel_passwords=None,
ssh_tunnel_private_keys=None, ssh_tunnel_private_key_passwords=None):
# Prepare the data to be sent in the form of a file
buf = BytesIO()
buf.write(json.dumps(dashboard_export).encode())
buf.seek(0)
# Prepare the form data
form_data = {
"formData": (buf, "dashboard_export.zip"),
"passwords": (None, json.dumps(passwords) if passwords else ""),
"overwrite": (None, str(overwrite).lower()),
"ssh_tunnel_passwords": (None, json.dumps(ssh_tunnel_passwords) if
ssh_tunnel_passwords else ""),
"ssh_tunnel_private_keys": (None, json.dumps(ssh_tunnel_private_keys)
if ssh_tunnel_private_keys else ""),
"ssh_tunnel_private_key_passwords": (None,
json.dumps(ssh_tunnel_private_key_passwords) if
ssh_tunnel_private_key_passwords else ""),
}
# Set the headers, including the authorization and CSRF tokens
headers = {
"Authorization": f"Bearer {auth_token}",
"X-CSRFToken": csrf_token,
}
# Send the POST request
response = requests.post(f"{api_url}/api/v1/dashboard/import/",
files=form_data, headers=headers)
# Check the response
if response.status_code == 200:
return response.json()
else:
return {"error": response.status_code, "message": response.text}
# Example usage
api_url = "http://your-superset-instance.com"
dashboard_export = {"your": "dashboard data"} # Replace with actual dashboard
export data
auth_token = "your_auth_token" # Replace with actual token if needed
csrf_token = "your_csrf_token" # Replace with actual CSRF token
result = import_dashboard(api_url, dashboard_export, auth_token, csrf_token)
print(result)
```
Make sure to replace `your_auth_token` and `your_csrf_token` with the actual
tokens you obtain from your Superset instance. The CSRF token can be retrieved
from the `/api/v1/security/csrf_token/` endpoint
<sup>[[1]](https://github.com/apache/superset/issues/32315)</sup>.
<!-- Dosu Comment Footer -->
*To continue talking to [Dosu](https://dosu.dev), mention @dosu.*
---
<sup>Help Dosu learn! Give it feedback: [Great
Response](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=great_response)
| [Irrelevant
Answer](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=irrelevant_answer)
| [Incorrect
Sources](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=incorrect_sources)
| [Too
Verbose](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=too_verbose)
|
[Hallucination](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=hallucination)
| [Bug
Report](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=bug_report)
|
[Other](https://app.dosu.dev/response-feedback/032ca7f9-a403-421c-9fff-c371be4e119f?feedback_type=other)</sup>
GitHub link:
https://github.com/apache/superset/discussions/32149#discussioncomment-12789083
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]