发布于 2026-01-06 0 阅读
0

使用 API 向 Dev、Hashnode 和 Medium 发布内容 使用 API 向 Dev、Hashnode 和 Medium 发布内容 YNWA DEV 的全球展示挑战赛,由 Mux 呈现:推介你的项目!

使用 API 将内容发布到 Dev、Hashnode 和 Medium。

使用 API 将内容发布到 Dev、Hashnode 和 Medium。

YNWA

由 Mux 主办的 DEV 全球展示挑战赛:展示你的项目!

醒目的主图展示了 Dev、Hashnode 和 Medium 的标志。

使用 API 将内容发布到 Dev、Hashnode 和 Medium。

本文旨在帮助您连接到这些网站的各个 API,以便通过编程方式发布、更新和使用这些服务。

为我的个人博客编写一个 Markdown 文件,然后复制粘贴到其他博客平台,这并不算太难……直到我需要编辑某些内容。一个简单的字体更改,却需要我打开四个不同的网站,在它们截然不同的用户界面中摸索,才能进行简单的修改。

这种做法很快就让人厌烦了。

我们是开发人员。将这些重复性任务自动化,会更有趣也更有益处。

Medium API

文档 - https://github.com/Medium/medium-api-docs

获取身份验证令牌

  1. 登录 Medium
  2. 导航至集成令牌
  3. 输入令牌描述,然后点击“获取集成令牌”按钮
生成集成令牌
Medium 的集成令牌面板

这将生成一个令牌,该令牌将包含在对 Medium API 的请求中。

获取已验证用户的详细信息

GET https://api.medium.com/v1/meMedium 提供获取已认证用户数据的端点。

以下是一个使用NodeJS的请求示例:

fetch('https://api.medium.com/v1/me', {
  method: 'GET',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer <AUTHENTICATION TOKEN HERE>',
  },
})
  .then(res => res.json())
  .then(res => console.log(JSON.stringify(res)))
Enter fullscreen mode Exit fullscreen mode

此端点返回的数据中包含一个 ID 值id。您需要保存此 ID 值,因为使用 NodeJS 创建 Medium 文章时需要用到它。

通过编程方式创建 Medium 文章

现在我们有了身份验证令牌和 ID,就可以使用该端点了POST https://api.medium.com/v1/users/{{authorId}}/posts。上面发布的文档详细介绍了需要哪些参数以及可以使用哪些参数。

此接口接受 Markdown 或 HTML 格式。您需要明确地将该contentFormat字段设置为Markdown 或markdownHTML格式之一html

以下是一个使用NodeJS的请求示例:

fetch('https://api.medium.com/v1/users/<USER-ID>/posts', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: 'Bearer <REPLACE WITH TOKEN GENERATED ABOVE>',
  },
  body: JSON.stringify({
    title: 'Liverpool FC',
    contentFormat: 'markdown',
    content: '# You can put Markdown here.\n***\nSee what it looks like?',
    canonicalUrl: 'http://jamietalbot.com/posts/liverpool-fc',
    tags: ['football', 'sport', 'Liverpool'],
    publishStatus: 'public',
  }),
})
  .then(res => res.json())
  .then(res => console.log(JSON.stringify(res)))
Enter fullscreen mode Exit fullscreen mode

如果返回 201 状态码,您现在应该可以在Medium.com上看到您的文章了。

可用端点:

以下是可用端点列表:

获取已认证用户的详细信息:

GET https://api.medium.com/v1/me

列出用户的出版物:

GET https://api.medium.com/v1/users/{{userId}}/publications

获取出版物的贡献者信息:

GET https://api.medium.com/v1/publications/{{publicationId}}/contributors

创建帖子:

POST https://api.medium.com/v1/users/{{authorId}}/posts

在某个出版物下创建帖子:

POST https://api.medium.com/v1/publications/{{publicationId}}/posts

上传图片:

POST https://api.medium.com/v1/images

遗憾的是,Medium 的公共 API 目前功能相当有限。我们可以创建和获取文章,但尚无法通过编程方式编辑文章。我希望这种情况最终能够有所改变,为程序员提供更好的工具来处理这些文章。

dev.to API

文档 - https://docs.forem.com/api/

获取身份验证令牌

这需要一个 dev.to 账号。请访问他们的文档并按照说明获取身份验证令牌。

使用 dev.to API 创建 dev.to 文章

他们的文档比 Medium 的公共 API 详尽得多。此接口的文档在此处。点击文章下拉箭头,即可查看他们接受的所有参数。

使用 NodeJS 的示例请求:

fetch('https://dev.to/api/articles', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'api-key': '<AUTHENTICATION TOKEN HERE>',
  },
  body: JSON.stringify({
    article: {
      title: 'Hello, World!',
      published: true,
      content: '# You can put Markdown here.\n***\n',
      tags: ['discuss', 'help'],
      series: 'Hello series',
    },
  }),
})
  .then(res => res.json())
  .then(res => console.log(JSON.stringify(res)))
Enter fullscreen mode Exit fullscreen mode

哈希节点 API

文档 - https://api.hashnode.com/

Hashnode 的 API 是一个 GraphQL API,这与我们之前在 Medium 和 dev.to 中使用的 REST API 不同。

他们的博客里有几篇博文讨论了他们的公共 API,但这些博文发布于 2019 年,所以文章中讨论的一些内容已经过时,例如 ` GLOBALand FOR_MEFeedType`。如果你想更好地了解相关内容,可以阅读这些博文,但请记住,很多参数都已被弃用。

获取身份验证令牌

您可以在 Hashnode 设置中创建/撤销令牌。登录您的帐户,转到开发者设置,然后生成新的令牌。

生成 Hashnode 身份验证令牌
生成 Hashnode 身份验证令牌

文档

我发现他们的GraphQL Playground才是权威信息来源。你需要浏览一下他们的 Playground,看看有哪些端点可用以及需要哪些输入。

Hashnode API Playground
Hashnode 的 GraphQL API Playground
哈希节点文档导航
哈希节点文档导航

使用 NodeJS 的示例请求:

fetch('https://api.hashnode.com', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: '<AUTHENTICATION TOKEN HERE>',
  },
  body: JSON.stringify({
    query:
      'mutation createStory($input: CreateStoryInput!){ createStory(input: $input){ code success message } }',
    variables: {
      input: {
        title: 'What are the e2e testing libraries you use ?',
        contentMarkdown: '# You can put Markdown here.\n***\n',
        tags: [
          {
            _id: '56744723958ef13879b9549b',
            slug: 'testing',
            name: 'Testing',
          },
        ],
        coverImageURL:
          'https://codybontecou.com/images/header-meta-component.png',
      },
    },
  }),
})
  .then(res => res.json())
  .then(res => console.log(JSON.stringify(res)))
Enter fullscreen mode Exit fullscreen mode

结论

既然我们可以通过编程方式向这三个网站发布内容,我们就可以构建一个应用程序,允许动态输入,例如使用文件读取器将.md文件传递给请求contentcontentMarkdown参数、编辑帖子等等。

请密切关注。我计划花些时间为开发者博​​主们开发一个界面,让他们能够轻松实现这个功能。在四个不同的网站上手动编辑每篇文章中的相同文本实在太麻烦了。我希望能够解决这个问题。

祝你好运!

请通过推特@codybontecou告诉我你对这篇文章的看法。

文章来源:https://dev.to/codybontecou/post-to-dev-hashnode-and-medium-using-their-apis-54k4