2

(lots of requests in the title, I know)
With Fiddler I got a request working. It does exactly what I want. When I try to reproduce it in python, it doesn't work. this is the Fiddler request:

POST https://ichthus.zportal.nl/api/v3/oauth/ HTTP/1.1
Host: ichthus.zportal.nl
Content-Type: application/x-www-form-urlencoded

username=138777&password=XXXXX&client_id=OAuthPage&redirect_uri=%2Fmain%2F&scope=&state=4E252A&response_type=code&tenant=ichthus

and this is the Python Requests code I tried:

import requests
endpoint = "https://ichthus.zportal.nl/api/v3/oauth/"
authData = {
    'username': '138777',
    'password': 'XXXXX',
    'client_id': 'OAuthPage',
    'redirect_uri': '/main/',
    'scope': '',
    'state': '4E252A',
    'response_type': 'code',
    'tenant': 'ichthus',
}
header = {
    'Host': 'ichthus.zportal.nl',
    'Content-Type': 'application/x-www-form-urlencoded',
}

response = requests.post(endpoint, data=authData, headers=header)
print response.headers
print response.status_code

With Request to Code I managed to get it to a python URLlib2 request. But I have never used URLlib before and I was wondering if it could be converted to a Python Requests request. This is the Python URLlib2 code:

def make_requests():
    response = [None]
    responseText = None

    if(request_ichthus_zportal_nl(response)):
        responseText = read_response(response[0])

        response[0].close()

def request_ichthus_zportal_nl(response):
    response[0] = None

    try:
        req = urllib2.Request("https://ichthus.zportal.nl/api/v3/oauth/")

        req.add_header("Content-Type", "application/x-www-form-urlencoded")

        body = "username=138777&password=XXXX&client_id=OAuthPage&redirect_uri=%2Fmain%2F&scope=&state=4E252A&response_type=code&tenant=ichthus"

        response[0] = urllib2.urlopen(req, body)

    except urllib2.URLError, e:
        if not hasattr(e, "code"):
            return False
        response[0] = e
    except:
        return False

    return True
Nathan
  • 900
  • 2
  • 10
  • 28

1 Answers1

2

Requests is a very popular external library but I wouldn't suggest relying on it.

I did notice that you are sending the following "data" with your request

authData = {
'username': '138777',
'password': 'XXXXX',
'client_id': 'OAuthPage',
'redirect_uri': '/main/',
'scope': '',
'state': '4E252A',
'response_type': 'code',
'tenant': 'ichthus',
}

But you specify that your data type is 'Content-Type': 'application/x-www-form-urlencoded' and you never encode your data. That may also be your culprit.

In addition. Requests is just a wrapper around urllib2 that automates a lot of the work.

Try using this with urllib/2

Urllib2.urlopen (url, data) 

With data being url encoded using

Urllib.urlencode ()

Refer to the python docs for more detail.

Derrick Cheek
  • 147
  • 1
  • 6