Retrieving measures or badges from the Web_API

(This question is unrelated to running the analysis, all of that works perfectly fine)

My problem is that I can’t figure out how to retrieve the badge or measures for any of the private projects in my org.
The weird thing is that retrieving a list of pull requests works like a charm, but when fetching a badge or measure it return status_code 200 and the SVG code for a “Project not found” image. Feel free to give it a try using the code below.

(ps: i’ve replaced the real project name with a dummy value in the code below)

import requests
from requests.exceptions import HTTPError
import os

API = "https://sonarcloud.io/api/";
HEADERS = { "Authorization": "Bearer " + os.environ["SONARCLOUD_TOKEN"] }

try:
    response = requests.get(
#        API + 'project_pull_requests/list',
        API + 'project_badges/quality_gate',
        params={'project' : 'orgname_projectname'}, 
        headers=HEADERS
    );
    response.raise_for_status();
    print(response.status_code)
    print(response.text)
except HTTPError as http_error:
    print(f"Something went wrong {http_error}")
200
<svg xmlns="http://www.w3.org/2000/svg" height="20" width="110">
    <!-- ERROR -->
    <linearGradient id="b" x2="0" y2="100%">
        <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
        <stop offset="1" stop-opacity=".1"/>
    </linearGradient>
    <clipPath id="a">
        <rect width="110" height="20" rx="3" fill="#fff"/>
    </clipPath>
    <g clip-path="url(#a)">
        <rect fill="#e05d44" height="20" width="110"/>
        <rect fill="url(#b)" height="20" width="110"/>
    </g>
    <g fill="#fff" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11" text-anchor="left">
        <text x="4" y="15" fill="#010101" fill-opacity=".3">Project not found</text>
        <text x="4" y="14">Project not found</text>
    </g>
</svg>

Ok, figured it out in the end …

Looks like the token the web_api docs are talking about isn’t the access token but a project specific token that’s available via the navigation/component API. Documentation is absolutely not clear about this :frowning:

def get_sonar_badge_token(project_key):
      url = f'{API}navigation/component?component={urllib.parse.quote(project_key)}'
      response = requests.get(url, headers=HEADERS)
      response.raise_for_status()
      return response.json()['badgeToken']