avatar

麦兜的小站

MDO.INK

  • 首页
  • 随笔
  • 知识库
  • 归档
  • 动态
  • 标签
  • 关于
Home 调用API保存并发布文章java示例
文章

调用API保存并发布文章java示例

Posted 2025-01-24 Updated 2025-01- 24
By power 已删除用户
7~9 min read

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);
}

}`

后端
License:  CC BY 4.0
Share

Further Reading

Jun 19, 2025

2024 年这 5 个 Node.js 后端框架最受欢迎

本文将介绍2024年的前5个Node.js后端框架,它们的特点和常见用例。 Express.js:经过测试的冠军 Express.js是Node.js最著名的后端框架之一。它是一个开源的Web应用程...

Jun 11, 2025

PHP Crop Image While Uploading with Cropper JS

In this post you can find how to use Cropper.js library for Crop image before uploading of image to the server using PHP script. In this tutorial you

Mar 6, 2025

Building a Simple Blog with Flight PHP

ThisguidewalksyouthroughcreatingabasicblogusingtheFlightPHPframework.You'llsetupaproject,defineroute...

OLDER

docker搭建mysql集群

NEWER

supervisor 安装、配置、常用命令

Recently Updated

  • 如何实现接口幂等性
  • 10个npm工具包
  • How to set up PHP7.4 on MacOS.
  • Automa:一键自动化,网页数据采集与工作流程优化专家Automa:解锁自动化
  • Mac 下用 brew 搭建 LNMP

Trending Tags

thinkphp clippings

Contents

©2025 麦兜的小站. Some rights reserved.

Using the Halo theme Chirpy