Authentication with API key randomly fails in Linux script

  • What Grafana version and what operating system are you using?
    9.01 on Oracle Linux 7.9

  • What are you trying to achieve?
    Export dashboard json files from a scheduled job running under cron

  • How are you trying to achieve it?
    I copied this script and generated an API key with ‘viewer’.
    I can run it on one system . I copy this script to another system (same Grafana, OS, API key) and run it I get an error because it thinks the key is ‘0000000’.
    Sometimes this happens on the first system as well.
    I thought I might use a service account. I have created one, but don’t know how to use it in the ‘curl’


for dashboard_uid in $(curl -sS -H "Authorization: Bearer $KEY" $HOST/api/search\?query\=\& | jq -r '.[] | select( .type | contains("dash-db")) | .uid'); do
    url=`echo $HOST/api/dashboards/uid/$dashboard_uid | tr -d '\r'`
    dashboard_json=$(curl -sS -H "Authorization: Bearer $KEY" $url)
    dashboard_title=$(echo $dashboard_json | jq -r '.dashboard | .title' | sed -r 's/[ \/]+/_/g' )
    dashboard_version=$(echo $dashboard_json | jq -r '.dashboard | .version')
    folder_title="$(echo $dashboard_json | jq -r '.meta | .folderTitle')"

    # You can export the files like this to keep them organized by organization:
    mkdir -p "$DASH_DIR/$ORG/$folder_title/dashboards_$ORG"
    echo $dashboard_json | jq -r {meta:.meta}+.dashboard  > $DASH_DIR/$ORG/$folder_title/dashboards_$ORG/${dashboard_title}_v${dashboard_version}.json
  • What happened?
    I get an error from the ‘jq’, but that is because the first ‘curl’ does not return any data.

  • What did you expect to happen?
    exports the dashboard json files to /usr/local/SAdata…

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

  • Did you receive any errors in the Grafana UI or in related logs? If so, please tell us exactly what they were.
    KEY and HOST are fine. I don’t include them for security reasons
    aamlxgmp001 mckenns /home/mckenns $ curl -sS -H “Authorization: Bearer $KEY” $HOST/api/search?query=&
    {“message”:“invalid API key”,“traceID”:“00000000000000000000000000000000”}

  • Did you follow any online instructions? If so, what is the URL?
    Local deployment using the OSS version of Granfana 9.01