# 获取JWT Auth Token

# 请求

  • Method: POST

  • Url: /jwt-api-token-auth/

  • Headers:

    • Content-Type: application/json
  • Body:

参数名 必填 类型 描述
username Y String 用户名
password Y String 密码
{
    "username": "username",
    "password": "password"
}

# 响应

  • Data type: application/json
字段名称 类型 描述
token String
{
    "token": "eyJhbG......abrCU"
}

# 获取 General Auth Token

# 请求

  • Method: POST

  • Url: /api-token-auth/

  • Headers:

    • Content-Type: application/json
  • Body:

参数名 必填 类型 描述
username Y String 用户名
password Y String 密码
{
    "username": "username",
    "password": "password"
}

# 响应

  • Data type: application/json
字段名称 类型 描述
token String
{
    "token": "gP4K......biHUoy"
}

# Python 示例

import json
import requests


url = "http://zkeco.xmzkteco.com:8097/jwt-api-token-auth/"
headers = {
    "Content-Type": "application/json",
}
data = {
    "username": "admin",
    "password": "admin"
}

response = requests.post(url, data=json.dumps(data), headers=headers)
print(response.text)

# Postman 示例

postman1 postman2

# Java 示例

public static String doPost(String httpUrl, String param) {

    HttpURLConnection connection = null;
    InputStream is = null;
    OutputStream os = null;
    BufferedReader br = null;
    String result = null;
    try {
        URL url = new URL(httpUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setConnectTimeout(15000);
        connection.setReadTimeout(60000);

        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestProperty("Content-Type", "application/json");da3efcbf-0845-4fe3-8aba-ee040be542c0
        os = connection.getOutputStream();
        os.write(param.getBytes());
        if (connection.getResponseCode() == 200) {

            is = connection.getInputStream();
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));

            StringBuffer sbf = new StringBuffer();
            String temp = null;
            while ((temp = br.readLine()) != null) {
                sbf.append(temp);
                sbf.append("\r\n");
            }
            result = sbf.toString();
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (null != br) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != os) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (null != is) {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        connection.disconnect();
    }
    return result;
}

public static String buildParams(String username, String password){
    String tmp = "{\"username\": \"" + username + "\"," +
        " \"password\": \""+ password + "\"}";
    return tmp;
}

public static void main(String[] args){
    // final String url = "http://zkeco.xmzkteco.com:8097/jwt-api-token-auth/";
    final String url = "http://zkeco.xmzkteco.com:8097/api-token-auth/";
    String param = buildParams("admin", "admin");
    String result = doPost(url, param);
}