SonarCloud API Authentication


I am trying to authenticate an API of the SonarCloud with Pyhton and I not have success.

I tried this options:

header -> Autorization: Basic “my token”
header -> Autorization: Basic base64(“my token”)
header -> Autorization: Basic “myuser:my token”
header -> Autorization: Basic base64(“myuser:my token”)
Using HTTPBasicAuth(of the Requests) -> HTTPBasicAuth(“user”, “token”)

Nothing work…

What is it the correct form of the authenticate?

Hi Uilian,

The following code snippet works with SonarCloud and a valid token (given a project_uuid parameter to set):

import json
import os
import requests

# API Call :
def get_project_info(project_uuid):
    # Provide your SonarCloud token using the env var SC_TOKEN
    sc_token_from_env = os.environ.get('SC_TOKEN')
    url = f'{project_uuid}'
    response = requests.get(url, auth=(str(sc_token_from_env), ''))
    if response.ok:
        data = json.loads(response.text)
        return data
        raise Exception(response)

result = get_project_info('olivier-schmitt_aws-ca')

It’s based in the requests package a very famous one but it should work with other similar packages.


Thanks for your answer.
I tried with your example and return 401 (not authorized).
The project that I try get content it is a organization project, but should works because I have full permission in the organization.


Is it public project or do you have a public project?



It is a private project from my organization.
I tried using your exemplo with a public project and not work, too.

Can you help me?


I need to use the Somar API. Can any of the Sonar team help me?

Hi @UilianM ,

apart from a little typo in the function call ( get_project_info('olivier-schmitt_aws-ca') -> get_project_info('olivier-schmitt_aws-cda') ) the python snippet from @Olivier_Schmitt works for me as well. could you maybe share a minimal feature how you try to authenticate? something like this:

import json
from pprint import pprint
import requests

FQDN = ''
API_PATH = '/api/favorites/search'

response = requests.get(FQDN+API_PATH, auth=('YOUR TOKEN GOES HERE', ''))

if response.ok:
    data = json.loads(response.text)
    raise Exception(response)

this api endpoint is rather easy and it will only succeed when you authenticate successfully

Hi Tobias.

See below.

The projects it is of the organization, but I have permission of the administrator on them.

if this api endpoint does not work with your token, the token is probably just wrong (or incomplete). try to create a new one in your profile

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.