You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Node.js中用@atproto/api获取Bluesky账号帖子遇问题求助

如何用@atproto/api v0.14.7获取Bluesky账号的帖子?

我在Node.js中使用@atproto/api(0.14.7版本)尝试获取任意Bluesky账号的帖子,但调用getPosts()时遇到了URI格式问题,试了多种URI都失败,也找不到该函数的正确使用示例。

我的代码示例

import { AtpAgent, AtpSessionEvent, AtpSessionData } from '@atproto/api'

const identifier = 'myuser.bsky.social';
const password = 'xxxx-xxxx-xxxx';
const bSkyService = 'https://bsky.social';

async function main () {
  const agent = new AtpAgent({
    service: bSkyService,
    persistSession: (evt: AtpSessionEvent, sess?: AtpSessionData) => {
      // store the session-data for reuse
    },
  });

  await agent.login({
    identifier,
    password,
  });

  const feedUri = `at://${agent.assertDid}/app.bsky.feed.generator`;
  const response = await agent.getPosts({ uris: [feedUri] });
  console.log('Response:', response);
}

main().catch(error => {
  console.log(error);
  process.exit(1);
})

尝试不同URI出现的错误

  • 'at://myuser.bsky.social' : XRPCError: Internal Server Error
  • at://${agent.assertDid}/app.bsky.feed.generator: XRPCError: Internal Server Error
  • 'myuser.bsky.social' : XRPCError: Error: uris/0 must be a valid at-uri
  • agent.assertDid: RPCError: Error: uris/0 must be a valid at-uri
  • 'at://bsky.social/myuser.bsky.social' : XRPCError: Internal Server Error

正确的解决方法

核心误解:getPosts()的用途

getPosts()函数的作用是根据给定的单个帖子AT URI列表,获取这些帖子的详细内容,它不是用来获取某个用户全部帖子的工具。你之前传入的都是用户身份或Feed生成器的URI,不符合该函数的参数要求。

获取用户全部帖子的正确方法

要获取某个用户的帖子,应该使用agent.api.app.bsky.feed.getAuthorFeed()方法,该方法接受用户的handle(如'example.bsky.social')或DID作为参数。

示例代码(登录版)

import { AtpAgent, AtpSessionEvent, AtpSessionData } from '@atproto/api'

const identifier = 'myuser.bsky.social';
const password = 'xxxx-xxxx-xxxx';
const bSkyService = 'https://bsky.social';
// 要获取帖子的目标用户handle
const targetUserHandle = 'example.bsky.social';

async function main () {
  const agent = new AtpAgent({
    service: bSkyService,
    persistSession: (evt: AtpSessionEvent, sess?: AtpSessionData) => {
      // 可选:存储会话数据以便复用
    },
  });

  await agent.login({ identifier, password });

  // 获取目标用户的帖子
  const response = await agent.api.app.bsky.feed.getAuthorFeed({
    actor: targetUserHandle,
    limit: 50, // 单次获取的最大帖子数,可调整
    // 可选:cursor参数,用于分页加载更多帖子
  });

  console.log('用户帖子:', response.data.feed);
}

main().catch(error => {
  console.error(error);
  process.exit(1);
})

示例代码(无需登录的公开API版)

如果只需要获取公开内容,可以使用公开API,无需登录:

import { AtpAgent } from '@atproto/api'

const bSkyPublicService = 'https://public.api.bsky.app';
const targetUserHandle = 'example.bsky.social';

async function main () {
  const agent = new AtpAgent({ service: bSkyPublicService });

  const response = await agent.api.app.bsky.feed.getAuthorFeed({
    actor: targetUserHandle,
    limit: 50,
  });

  console.log('用户公开帖子:', response.data.feed);
}

main().catch(error => {
  console.error(error);
  process.exit(1);
})

如何正确使用getPosts()(如果需要)

如果你确实需要用getPosts()获取特定帖子,需要传入单个帖子的AT URI,格式为:

at://[用户DID]/app.bsky.feed.post/[帖子ID]

比如:

const postUris = ['at://did:plc:abc123/app.bsky.feed.post/def456'];
const response = await agent.getPosts({ uris: postUris });

内容的提问来源于stack exchange,提问作者Andre M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 00:02:36