Accessing Snap REST Api via Python Requests

I am developing a front end for snap… I am trying to access the REST API via python requests but I keep getting a ConnectionRefusedError [Errno 111] when executing the following code:


I assume this is because I haven’t authenticated with the rest API. When accessing the API via a terminal you can authenticate like so:

curl -L https://localhost:8181/v2/snaps -u

However, I cannot for the life of me figure out how to authenticate over python requests. I have dug around in both the requests documentation (which provides authentication protocols, but no relevant ones) and the snap REST API documentation (which does not provide the necessary examples in python for me to determine how I might go about authentication).

So, how on earth do I authenticate with the snap REST API over python requests?

Are you trying to communicate with the snapd socket from inside a strictly confined snap?

No, I am not even trying to access the API from inside a snap at all. This python script is not a snap. I simply trying to get information on snap packages from my script.

Snapd uses polkit to authenticate over the http unix socket, so you will have to look into how to do that via Python. I’m not personally aware of how that works, but looking at our snapd code, it uses an HTTP header X-Allow-Interaction, so perhaps that’s a starting point.

Hi @Stasiu_Nowak,

I am also trying to figure out the same thing, but I am running the python script through a snap. Did you manage to figure out how?