Generated API token doesn't work accessing the datasources

Hi Grafana team

  • What Grafana version and what operating system are you using?
    grafana version 9.4.7 together with portainer and influx runs within a docker container which runs on Ubuntu 20.04LTS server on a Raspi. The GUI, datasources and dashboards work fine.

  • What are you trying to achieve?
    I’m trying to create an API key remotely and use it to browse datasources. I’m trying to create a backup of all datasources, dashboards for a CI/CD workflow. The code that’s supposed to actually work is:

for uid in $(echo $datasources | jq -r '.[] | .uid'); do
  uid="${uid/$'\r'/}" # remove the trailing '/r'
  curl -s -H "Authorization: Bearer $apikey" -X GET "$grafanaurl/datasources/uid/$uid" | jq > grafana-datasource-$uid.json
  slug=$(cat grafana-datasource-$uid.json | jq -r '.name')
  mv grafana-datasource-$uid.json grafana-datasource-$uid-$slug.json # rename with datasource name and id
  echo "Datasource $uid exported"
cd ../../..
source .env

export token=$GRAFANA_TOKEN
export grafanaurl_token=http://$PV_RASPI_IP:3000/api
export grafanaurl_admin=http://$GRAFANA_ADMIN_USER:$GRAFANA_ADMIN_PASSWORD@$PV_RASPI_IP:3000/api

#create organization
output=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"apiorg"}' $grafanaurl_admin/orgs 2>/dev/null)
echo "apiorg creation: $output"
orgId=$(echo "$output" | jq -r '.orgId' )
echo "orgId: $orgId"

#change organization 
output=$(curl -X POST $grafanaurl_admin/user/using/$orgId 2>/dev/null)
echo "orga change: $output"

#create api key
output=$(curl -X POST -H "Content-Type: application/json" -d '{"name":"apikey", "role": "Admin"}' $grafanaurl_admin/auth/keys 2>/dev/null)
echo "create api key: $output"
apikey=$(echo "$output" | jq -r '.key' )
apikey_id=$(echo "$output" | jq -r '.id' )
echo "apikey=$apikey and it's id=$apikey_id"

cd grafana/provisioning/datasources
datasources=$(curl -s -H "Authorization: Bearer $apikey" -X GET $grafanaurl_token/datasources)
echo $datasources
  • What happened?
    The result of the script is:
apiorg creation: {"message":"Organization created","orgId":48}
orgId: 48
orga change: {"message":"Active organization changed"}
create api key: {"id":47,"name":"apikey","key":"eyJrIjoiN1NlV1ZMc3dWaVZmNzdHR2xnbWpnaDVIcmRqRzhNT2IiLCJuIjoiYXBpa2V5IiwiaWQiOjQ4fQ=="}
apikey=eyJrIjoiN1NlV1ZMc3dWaVZmNzdHR2xnbWpnaDVIcmRqRzhNT2IiLCJuIjoiYXBpa2V5IiwiaWQiOjQ4fQ== and it's id=47
  • What did you expect to happen?
    I actually did expect to have all datasources in the array. This actually works, when I’m using a manually created API token that I generate on the GUI. But not with script-generated tokens. I’m not aware of any differences.

  • Can you copy/paste the configuration(s) that you are having problems with?
    sure, see above

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.

no GUI errors, the GUI works just fine.

  • List item

  • Did you follow any online instructions? If so, what is the URL?