6

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.

2
  • There are issues with the python version and some http based services. May be that OAuth2 doesn't work with the python from OSGeo? Mainly if you are under ms windows... Commented Jun 29, 2016 at 7:30
  • I've made some tests : - https requests toward another website or API did work fine. - http request to my API work - Requests to my API that aren't the one asking for a token (the one that have the token in the header and ask for content) work in http but not in https. So the problem must be the ssl certificate signature verification. The organism that has signed our API certificate must be absent of the list used by QGIS.
    – El Theo
    Commented Jun 30, 2016 at 15:43

1 Answer 1

2

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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.