A
A
Alexey Samoilov2015-04-17 17:53:31
Python
Alexey Samoilov, 2015-04-17 17:53:31

How to access aliexpress epn API?

Good evening everyone.
Has anyone used the epn aliexpress API?
How to build a query?
Extract from documentation:

All requests to the API are sent as an HTTP POST request to the api.epn.bz/json URL. Request parameters are passed as a JSON string as RAW POST DATA data. The response is also given in JSON format.
The request structure is:
{
user_api_key = $your_api_key
user_hash = $your_deep_link_hash
api_version = $client_library_version
requests = $requests_to_process
}
All parameters are required. The 'user_api_key' and 'user_hash' data are taken from the affiliate program's personal account (the first one is in the user's profile, and the second one is in the list of creatives), 'api_version' describes the version of the client library (the current description corresponds to version "2").
The $requests_to_process structure contains a list of requests to be processed in batch mode and looks like this:
{
$req_name_1 = $req_data_1
$req_name_2 = $req_data_2
// ... //
$req_name_n = $req_data_n
}
Thus, when sending a batch for each request you need to specify a unique block name, and in the returned result, look for the necessary data by this name. Each request description has the form:
{
action = $action
$param_1 = $value_1
$param_2 = $value_2
// ... //
$param_n = $value_n
}
The list of available options depends on the value of 'action'. Options that are not supported for the given 'action' will simply be ignored.

For some reason, there are no examples of requests.
I try this approach, but I don’t know how to form the essence of the request, what am I doing wrong?
import urllib
import urllib2

headers = {'User-Agent' : 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'}
values = {'user_api_key': 'My_key',
          'user_hash': 'My_hash',
          'api_version': '2'
        # А здесь-то что? нужно?
}

data = urllib.urlencode(values)
req = urllib2.Request(url,data)

response = urllib2.urlopen(req)
print response.read()

Thank you.
UPD:
The question is more correct to reformulate - "How to send HTTP POST request in JSON format using Python 2.x".
What you need to know:
1. Before sending data, you need to serialize it to json
2. You need to send data indicating that it is json
Solutions:
1. Using the requests library (it does everything itself except for serialization):
import json
import requests

## URL, куда мы обращаемся
url = 'http://example.com'

## Наши данные (словарь)
values = {'id': '12356', 'desc': 'something'}

## Делаем запрос, данные сериализуем json.dumps(x)
r = requests.post(url, data=json.dumps(values), headers=headers)

## Считываем в словарь
dict = r.json()

2. Using only the standard library:
import json
import urllib2

## URL, куда мы обращаемся
url = 'http://example.com'

## Наши данные (словарь)
values = {'id': '12356', 'desc': 'something'}

## Создаем запрос и добавляем к нему header с указанием на то, что это json
r = urllib2.Request(api_url)
r.add_header('Content-Type', 'application/json')

## Делаем запрос, данные сериализуем json.dumps(x)
response= urllib2.urlopen(r, json.dumps(values))

## Данные будут получены в формате, с которым работает API. В нашем случае это опять JSON, мы считаем его в словарь
dict = json.loads(response.read())

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-04-20
@tibhar940

First, try using a wrapper around urllib - the requests library. It has a friendlier, higher-level API and some useful perks.
Secondly, I don't see where your data is being serialized into json.
In general, it should look something like this:

r = requests.post(url, data=jsonify(values), headers=headers)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question