禅道登录-调用API方式
创始人
2024-03-16 14:31:33
0

禅道提供了API机制方便于大家和其他的系统进行集成,API机制也都是基于http协议的,返回的数据以json格式存储。禅道的API都是需要先登录后才能进行接口调用(登录返回的cookie需要在之后的每次请求中携带用于验证身份信息)。网上关于禅道API调用机制的说明相对较少,接下来我会从postman调用和Java代码两种方式来体现禅道整个登录过程

注意:个别信息模糊展示比较私密不便于公开,请见谅!!

目录

流程图

获取session

登录-验证用户身份

获取cookie

参考代码--doGet()


流程图

获取session

首先调用禅道提供的获取SessionID的API

postman:

接口返回的data中的sessionID对应的value值就是我们需要获取的sessionID值,用于后续禅道登录验证

Java代码:

//调用禅道接口获取sessionID
String session = doGet("http://127.0.0.1/zentao/api-getSessionID.json", null, false);// json解析器-GSON
JsonParser parse = new JsonParser();//将接口返回的字符串解析成json格式
JsonObject jsonSession = (JsonObject) parse.parse(session);//将json格式中的data解析出来
JsonObject jsonObj = (JsonObject) parse.parse(jsonSession.get("data").getAsJsonPrimitive().getAsString());//获取key为sessionID的值
String sessionID = jsonObj.get("sessionID").toString();

登录-验证用户身份

现在我们就可以使用sessionID来用户身份验证进行登录了,登录的时候需要提供用户名和密码,变量名如下:account,password

postman:

将上一步获取到的sessionID放到url地址中,在Params以key、value的形式放置登陆的用户名和密码

Java代码:

//存储登录需要使用到的信息
Map map = new HashMap();
//账号
map.put("account", "admin");
//密码
map.put("password", "A@789dmin");
//sessionID
map.put("zentaosid", sessionID);//登录url
String loginResult = doGet("http://zentao.dmsd" +".tech//zentao/user-login-XXXX=.json", map, true);

获取cookie

登录成功之后我们把登录返回的cookie获取出来,用于调用其他API的携带信息了

说明:在解析响应体信息时,

postman:

在上一步登录之后返回的Cookie中包含了名为【zentaosid】的key、value,其中zentaosid为cookie名,相应的value为值,我们只需要获取出来便可以进行其他API的调用了

Java代码:

/**
* 用来存取cookies信息的变量.
*/
private static CookieStore cookieStore;//获取登录过后的cookie 存入 cookieStore对象
public static String GetCookies(HttpGet get) throws IOException {String result = null;try {if (null == cookieStore) {cookieStore = new BasicCookieStore();}//获取响应CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();//执行请求CloseableHttpResponse response = httpClient.execute(get);result = EntityUtils.toString(response.getEntity(), "utf-8");// 获取cookies信息List cookies = cookieStore.getCookies();//解析出zentaosidfor (Cookie cookie : cookies) {String name = cookie.getName();String value = cookie.getValue();if (name.equals("zentaosid")) {Constant.ZENTAOID = value;}}} catch (IOException e) {e.printStackTrace();}return result;
}

参考代码--doGet()

作用:执行url请求调用

//向指定url发起请求,可携带参数,并选择是否携带cookiepublic static String doGet(String url, Map params, boolean isCookie) {//获取httpclient客户端CloseableHttpClient httpclient = HttpClients.createDefault();String resultString = "";CloseableHttpResponse response = null;try {URIBuilder builder = new URIBuilder(url);if (null != params) {for (Object key : params.keySet()) {String keyStr = key.toString();String result = params.get(key).toString();builder.setParameter(keyStr, result);}}//Get请求HttpGet get = new HttpGet(builder.build());//判断是否需要传入cookie//是:调用GetCookies()方法获取cookie//否:执行请求if (!isCookie) {//执行请求response = httpclient.execute(get);//200表示接口调用成功if (200 == response.getStatusLine().getStatusCode()) {//HttpEntity表示http的request和resposne实体,它由消息头和消息体组成。//从HttpEntity中可以获取http请求头和回应头,也可以获取http请求体和回应体信息。HttpEntity entity = response.getEntity();resultString = EntityUtils.toString(entity, "utf-8");}} else {//调用GetCookies()方法获取cookieresultString = GetCookies(get);}} catch (Exception e) {e.printStackTrace();} finally {if (null != response) {try {response.close();} catch (IOException e) {e.printStackTrace();}}if (null != httpclient) {try {httpclient.close();} catch (IOException e) {e.printStackTrace();}}}return resultString;}

参考链接:zentaoPHP二次开发简介 - zentaoPHP二次开发 - 易软天创开发者中心

相关内容

热门资讯

【PdgCntEditor】解... 一、问题背景 大部分的图书对应的PDF,目录中的页码并非PDF中直接索引的页码...
在Word、WPS中插入AxM... 引言 我最近需要写一些文章,在排版时发现AxMath插入的公式竟然会导致行间距异常&#...
监控摄像头接入GB28181平... 流程简介将监控摄像头的视频在网站和APP中直播,要解决的几个问题是:1&...
修复 爱普生 EPSON L4... L4151 L4153 L4156 L4158 L4163 L4165 L4166 L4168 L4...
protocol buffer... 目录 目录 什么是protocol buffer 1.protobuf 1.1安装  1.2使用...
Windows10添加群晖磁盘... 在使用群晖NAS时,我们需要通过本地映射的方式把NAS映射成本地的一块磁盘使用。 通过...
【前端】‘??‘与‘||‘有什... 0 问题 经常写const data = res.data.a ?? ''或者const d...
ChatGPT 怎么用最新详细... ChatGPT 以其强大的信息整合和对话能力惊艳了全球,在自然语言处理上面表现出了惊人...
Fluent中创建监测点 1 概述某些仿真问题,需要创建监测点,用于获取空间定点的数据࿰...
educoder数据结构与算法...                                                   ...