[GIS] Sending a https request from a QGIS plugin using urllib2

http-postpyqgispythonqgis-plugins

I'm developing a QGIS plugin that needs to send https request to an API.
I have to use urllib2 (see below if you want to know why I don't just use the requests package)

The first request I need to make is a POST one, with id in the header, so the API would grant me an access token (according to the OAuth2 protocol)
I have this portion of script, that works when executed alone, but doesn't as soon as I integrate it in my QGIS plugin script, it gives me a weird URLError (code 10054 : the distant host had to close a connexion) but I suspect QGIS doesn't event send the request at all, for it isn't an answer that the API gives.

import urllib
import urllib2

myIDs = "Basic hjNjkJCOHdrUncN4RVV"
url = 'https://id.mygreatapi.com/oauth/token'
values = {"grant_type":"client_credentials"}
data = urllib.urlencode(values)
header = {"Authorization": myIDs}
req = urllib2.Request(url,data, header)
try:
    handle = urllib2.urlopen(req)
    content = handle.read()
    print(content)
except urllib2.HTTPError as e:
    code = e.code
    texte = e.read()
    print(code)
    print(texte)

Note that when I copy a valid token into my QGIS plugin script and then make GET requests to the API using http, it works. So I guess the problem comes from the https.

Any ideas why QGIS wouldn't send this request ?

Why don't I use requests library ?
It is necessary that the plugin could be installed by anyone (especially somebody without admin rights on his computer, and no programming knowledge whatsoever) so I can't expect people to use their OSGeo and use pip install.
The solutions proposed in this page being totally out of my competences, I considered that I have to stick with the packages that are built-in the python version QGIS uses.

Best Answer

So, it was a ssl certificate validation problem. The python used by QGIS didn't recognize the organism that had signed my API's ssl certificate.

There were a possible work around here (telling urllib2 to ignore certificate validation) but it calls urllib2.HTTPSHandler() which has been implemented in a python version posterior to the one QGIS uses.

The true solution would have been to package the certificate inside my plugin and tell urllib2 where to look with the 'cafile' and 'capath' parameters of the urlopen function, but there again, these parameters didn't exist in python 2.7.5.

So there is actually no solution for the question I had asked. (For information, I got around it using Qt's QNetworkAccessManager instead of urllib2)

Related Question