Home > Software engineering >  Python Request to Get Token
Python Request to Get Token

Time:03-15

I am new to python API. I am trying to use Request POST method to get access token.

According to their API document,

HTTP Request POST /api/auth

curl -X POST "/api/auth"
  -F email=my_email 
  -F api_key=my_api_key

from https://api.aupropertyreport.com/doc/#authentication

I wrote like this:

auth_url = 'https://aupropertyreport.com/api/auth'

response = requests.post(auth_url, data = {

                        'email':'[email protected]',
                        'api_key':auproperty_api,
                        })
token = response.json()

And it seems not working if anyone knows how to solve this?

Error Message:

{'err_code': '9001',
 'err_title': '\u51fa\u9519\u5566',
 'err_msg': 'success',
 'data': [],
 'has_next_page': False,
 'updated': '2022-03-15 17:58:23'}

If successful, it should return something like this: (as an example from its API documentation)

{
    "data": {
        "token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vbHVtZd4tYXBpLWRlbW8uZGV1L2FwaS9hdXRob3JpemF0aW9ucyIsImlhdCI6MTQ4Mzk3NTY5MywiZXhwIjoxNDg5MTU5NjkzLCJuYmYiOjE0ODM5NzU2OTMsImp0aSI6ImViNzAwZDM1MGIxNzM5Y2E5ZjhhNDk4NGMzODcxMWZjIiwic3ViIjo1M30.hdny6T031vVmyWlmnd2aUr4IVM9rm2Wchxg5RX_SDpM",
        "expired_at": "2017-03-10 15:28:13",
        "refresh_expired_at": "2017-01-23 15:28:13"
    }

Thanks

CodePudding user response:

Try with this code. The details should go into header and not data.

headers["Accept"] = "application/json"
headers["email"] = "<email-id>"
headers["api_key"] = "<api_key>"
headers["Content-Type"] = "application/json"    
resp = requests.post(url, headers=headers)

CodePudding user response:

The CURL command shows the option -F which refers to form-data in POST request. However, python requests doesn't know it should send data as multipart/form-data.

Add a header field in your post request to use Content-Type: multipart/form-data.

  • Related