Hello Grafana community,
I’m using Grafana 10.2 version on linux and I trying to create a python program to authorize a user to view a dashboard. Seem simple, but facing difficulties since many days.
Today, I can import a dashboard via the HTTP API and I can create the user, but impossible to add permission to the user the view the imported dashboard.
I follow the API reference guide and today the program finish with success but all permissions are removed except the Admin.
see the pictures of the permissions, before and after my program.
here below my program:
from requests.auth import HTTPBasicAuth
import json
import subprocess
grafana_url_api = 'http://192.168.YYY.XXX:3000/api'
auth = ('admin', 'zzzzzzzz')
grafana_token = "XXXXXXXXXXXXXXXXXXXXXXXX"
router = "util7"
url= grafana_url_api + "/users/lookup?loginOrEmail=" + router
response = requests.get(url, auth=auth)
if response.status_code == 200:
myuser_data = response.json()
user_uid = myuser_data.get("id")
print(f"User UID for user {router}: {user_uid}")
else:
print(f"Failed to retrieve user UID. Status Code: {response.status_code}, Response: {response.text}")
*#provide permission to user to dashboard*
myheader = {
'Authorization': f"Bearer {grafana_token}",
'Content-Type': 'application/json',
'Accept': 'application/json',
}
url = grafana_url_api + f'/dashboards/uid/{router}-R/permissions'
print(url)
#Get the current dashboard permissions*
response = requests.get(url, headers=myheader)
if response.status_code == 200:
dashboard_data = response.json()
print(dashboard_data)
new_user_entry = {
'userId': user_uid,
'permission': 1,
}
update_response = requests.post(url, headers=myheader, json=new_user_entry)
if update_response.status_code == 200:
print(f'Permission for {router} updated successfully to Viewer')
else:
print(f'Failed to update permission: {update_response.text}')
else:
print(f'Failed to fetch dashboard data: {response.text}')
Do you have any glue to solve the issue?
I really need help on this. Thanks all of you.