Python script to generate a usage report using webapi

we use sonarcloud and all repos are private. I am trying to write a python script to pull list of projects on-boarded so far for scanning

r = requests.get(‘http://sonarcloud.io/api/projects/search?organization=my-org’,headers={'Authorization’: ‘’})
I get following error {‘errors’: [{‘msg’: ‘Insufficient privileges’}]}
could someone help pls.

thanks
HT

Hi @hkstKM,

And welcome to the community! Here’s a snippet you could use.
You should definitely use https and provide a token.
I hope this helps!

#!/usr/bin/python3

import requests

url = 'https://sonarcloud.io/api/projects/search?organization=yourorganization'
token = 'yourtoken.....'

session = requests.Session()
session.auth = token, ''
call = getattr(session, 'get')
res = call(url)
print(res.status_code)
print(res.content)
1 Like

Thanks Alex - problem solved.

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