Request所属依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>fluent-hc</artifactId>
<scope>test</scope>
</dependency>
pat获取 :控制台左下角的用户图标 -> 我的 -> 个人令牌 -> 新建
categories :分类列表
tags:标签列表
cover:封面图片地址
title:标题
visible:可见性("PRIVATE":"私有","PUBLIC":"公开")
@Value("${pat}")
String pat;
void postsAndPublish(String videoUrl,String title,String imgUrl) throws Exception {
UUID uuid = UUID.randomUUID();
JSONArray categories = new JSONArray();
categories.add("category-xGmNH");
categories.add("category-vqqut");
JSONArray tags = new JSONArray();
tags.add("tag-upnkj");
JSONObject req = new JSONObject();
JSONObject content = new JSONObject();
content.put("content","<p style=\"\"><iframe src=\"" + videoUrl + "\" width=\"100%\" height=\"600px\" frameborder=\"0\" allowfullscreen=\"true\" framespacing=\"0\" style=\"display: inline-block\"></iframe></p>");
content.put("raw","<p style=\"\"><iframe src=\"" + videoUrl + "\" width=\"100%\" height=\"600px\" frameborder=\"0\" allowfullscreen=\"true\" framespacing=\"0\" style=\"display: inline-block\"></iframe></p>");
content.put("rawType","HTML");
req.put("content",content);
JSONObject post = new JSONObject();
post.put("apiVersion","content.halo.run/v1alpha1");
post.put("kind","Post");
JSONObject metadata = new JSONObject();
metadata.put("annotations",new JSONObject().put("content.halo.run/preferred-editor","default"));
metadata.put("name", uuid);
post.put("metadata",metadata);
JSONObject spec = new JSONObject();
JSONObject excerpt = new JSONObject();
excerpt.put("autoGenerate",true);
excerpt.put("raw","");
spec.put("excerpt",excerpt);
spec.put("allowComment",true);
spec.put("cover",imgUrl);
spec.put("deleted",false);
spec.put("htmlMetas",new JSONArray());
spec.put("pinned",false);
spec.put("priority",0);
spec.put("publish",false);
spec.put("slug",String.valueOf(System.currentTimeMillis()));
spec.put("template","");
spec.put("title",title);
spec.put("visible","PRIVATE");
spec.put("categories",categories);
spec.put("tags",tags);
post.put("spec",spec);
req.put("post",post);
String postsUrl = "http://localhost/apis/api.console.halo.run/v1alpha1/posts";
Request.Post(postsUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + pat)
.bodyString(req.toJSONString(), ContentType.APPLICATION_JSON).execute().returnContent().asString(StandardCharsets.UTF_8);
String publishUrl = postsUrl + "/" + uuid + "/publish";
Request.Put(publishUrl)
.addHeader("Content-Type", "application/json")
.addHeader("Authorization", "Bearer " + pat)
.execute().returnContent().asString(StandardCharsets.UTF_8);
}
}`