Access example of JD API interface based on HTTP/HTTPS protocol

    JD API is a RESTful API based on HTTP/HTTPS protocol, used to access JD's open data resources. Developers can use this interface to obtain information such as products, prices, inventory, shipping costs, and reviews, and to perform operations such as order transactions and logistics tracking. Before accessing the JD.com API, you need to apply for API Key and Secret Key first . The application address is: https://jos.jd.com/apiconsole/register/index.action.

After obtaining the API Key and Secret Key, you can follow the steps below to access:

1. Establish a connection and authorize verification

First of all, before requesting the JD API, a connection needs to be established and authorization verification performed. There are two authorization methods: OAuth2.0 and AppKey authentication. Choose the verification method that suits you according to the actual situation. Specific steps are as follows:

(1) OAuth2.0 authorization verification

When using the OAuth2.0 authorization verification method, you need to obtain an Access Token first. The acquisition methods include authorization code mode, password mode, refresh token mode, etc. Authorization code mode and password mode can be obtained by referring to official documents. After obtaining the Access Token, you can use the Token to access the API interface. Example request:

```python
import requests

# Set request header information

headers = {"Authorization": "Bearer <access_token>"}

# Construct request URL

url = "https://api.jd.com/routerjson"

# Construct request parameters

params = {
        "method": "jingdong.afsservice.channel.get",
        "app_key": "your_app_key",
        "timestamp": "2018-07-31 16:54:34",
        "v": "2.0",
        "360buy_param_json": "{\"type\": \"afs_reason\"}"
    }

# send request

response = requests.post(url, params=params, headers=headers)
```

(2) AppKey Verification

When using the AppKey authentication method, you need to pass the AppKey and AppSecret in the request header information. Example request:

```python
import requests

# 设置请求头部信息
headers = {"Content-Type": "application/x-www-form-urlencoded",
           "AppKey": "<your_app_key>",
           "AppSecret": "<your_secret_key>"}

# 构造请求URL
url = "https://api.jd.com/routerjson"

# 构造请求参数
params = {
        "method": "jingdong.afsservice.channel.get",
        "app_key": "your_app_key",
        "timestamp": "2018-07-31 16:54:34",
        "v": "2.0",
        "360buy_param_json": "{\"type\": \"afs_reason\"}"
    }

# 发送请求
response = requests.post(url, params=params, headers=headers)
```

2. Call the API interface to obtain data

After the access authorization is successful, you can use the API Key and Secret Key to access the JD API interface to realize business functions. Take obtaining the price and inventory of a product as an example, a specific request example:

```python
import requests
import json

# Construct request URL

url = "https://router.jd.com/api"

# Construct request parameters

data = {
        "skuIds": "123456",
        "area": "1_72_4127_0",
        "cat": "",
        "venderId": "",
        "shopId": "",
        "pSize": "",
        "page": "",
        "callback": "",
        "locationId": ""
    }
params = {
        "method": "jingdong.price.read.query",
        "app_key": "<your_app_key>",
        "sign_method": "md5",
        "access_token": "<your_access_token>",
        "timestamp": "2019-05-07 15:26:00",
        "format": "json",
        "v": "2.0",
        "360buy_param_json": json.dumps(data)
    }

# send request

response = requests.post(url, params=params)

# Parse the returned result
result = response.json()
```

The above is an example of a simple API interface call, and it needs to be adjusted according to specific business needs in actual use.

Guess you like

Origin blog.csdn.net/onebound_linda/article/details/130948146
Recommended