mastodon
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCall the Mastodon REST API with . Two connector credentials are
injected: (the instance, e.g. )
and . Every request sends the header
.
curl + jq$MASTODON_BASE_URLhttps://mastodon.social$MASTODON_ACCESS_TOKENAuthorization: Bearer $MASTODON_ACCESS_TOKENErrors come back as JSON — show it verbatim.
() means the token is wrong/revoked → the user
must re-connect the Mastodon connector. Posting needs the token to have the
(or ) scope.
{"error":"<message>"}401"The access token is invalid"writewrite:statusesAlways confirm the token + account first (also gives the account you
need to list your own toots):
idbash
curl -sS "$MASTODON_BASE_URL/api/v1/accounts/verify_credentials" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
| jq '{id, username, acct, display_name, followers: .followers_count, statuses: .statuses_count}'使用调用Mastodon REST API。会注入两个连接器凭据:(实例地址,例如)和。每个请求都会发送请求头。
curl + jq$MASTODON_BASE_URLhttps://mastodon.social$MASTODON_ACCESS_TOKENAuthorization: Bearer $MASTODON_ACCESS_TOKEN错误信息会以JSON格式返回——请直接展示该信息。错误()表示令牌错误/已撤销→用户必须重新连接Mastodon连接器。发布内容需要令牌拥有(或)权限范围。
{"error":"<message>"}401"The access token is invalid"writewrite:statuses请先确认令牌和账户信息(这同时会返回你列出自己toot所需的账户):
idbash
curl -sS "$MASTODON_BASE_URL/api/v1/accounts/verify_credentials" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
| jq '{id, username, acct, display_name, followers: .followers_count, statuses: .statuses_count}'Post a toot
发布toot
Confirm with the user before posting publicly. Default to
unless they say post publicly; use only on request.
visibilityunlistedpublicbash
STATUS_TEXT="Hello fediverse 👋 #introductions"
curl -sS -X POST "$MASTODON_BASE_URL/api/v1/statuses" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
-H "Idempotency-Key: $(uuidgen)" \
--data-urlencode "status=$STATUS_TEXT" \
--data-urlencode "visibility=unlisted" \
--data-urlencode "language=en" \
| jq '{id, url, visibility, created_at}'- is one of
visibility,public,unlisted,private.direct - Optional params: (content warning),
spoiler_text(reply),in_reply_to_id,sensitive=true(ISO 639-1).language - (any unique string;
Idempotency-Keyhere) prevents duplicate posts if the request is retried within ~1h. Useuuidgenso hashtags, emoji and newlines in the text are encoded correctly.--data-urlencode - Default post length is 500 chars (instance-configurable); longer text →
.
422 {"error":"Validation failed: Text ..."}
公开发布前请与用户确认。默认为(未列出),除非用户明确要求公开发布;仅在用户请求时使用(公开)。
visibilityunlistedpublicbash
STATUS_TEXT="Hello fediverse 👋 #introductions"
curl -sS -X POST "$MASTODON_BASE_URL/api/v1/statuses" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
-H "Idempotency-Key: $(uuidgen)" \
--data-urlencode "status=$STATUS_TEXT" \
--data-urlencode "visibility=unlisted" \
--data-urlencode "language=en" \
| jq '{id, url, visibility, created_at}'- 可选值为
visibility、public、unlisted、private。direct - 可选参数:(内容警告)、
spoiler_text(回复目标ID)、in_reply_to_id(标记为敏感内容)、sensitive=true(ISO 639-1格式语言代码)。language - (任意唯一字符串;此处使用
Idempotency-Key生成)可防止请求在约1小时内重试时重复发布。使用uuidgen确保文本中的话题标签、表情符号和换行符被正确编码。--data-urlencode - 默认帖子长度为500字符(可由实例配置);文本过长会返回错误。
422 {"error":"Validation failed: Text ..."}
List my recent toots + engagement
列出我最近的toot及互动数据
Use the from :
idverify_credentialsbash
ACCT_ID="14715" # from verify_credentials
curl -sS "$MASTODON_BASE_URL/api/v1/accounts/$ACCT_ID/statuses?limit=20&exclude_replies=true&exclude_reblogs=true" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
| jq '.[] | {id, url, boosts: .reblogs_count, favs: .favourites_count, replies: .replies_count, created_at}'limitonly_mediapinnedtagged=<hashtag>使用返回的:
verify_credentialsidbash
ACCT_ID="14715" # 来自verify_credentials的结果
curl -sS "$MASTODON_BASE_URL/api/v1/accounts/$ACCT_ID/statuses?limit=20&exclude_replies=true&exclude_reblogs=true" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" \
| jq '.[] | {id, url, boosts: .reblogs_count, favs: .favourites_count, replies: .replies_count, created_at}'limitonly_mediapinnedtagged=<hashtag>Delete a toot
删除toot
bash
curl -sS -X DELETE "$MASTODON_BASE_URL/api/v1/statuses/STATUS_ID" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" | jq '{id, deleted: true}'Deleting returns the status with its source so you can delete-and-redraft.
= not yours or already gone.
text404 {"error":"Record not found"}bash
curl -sS -X DELETE "$MASTODON_BASE_URL/api/v1/statuses/STATUS_ID" \
-H "Authorization: Bearer $MASTODON_ACCESS_TOKEN" | jq '{id, deleted: true}'删除操作会返回包含原始的状态信息,方便你删除后重新编辑发布。错误表示该toot不属于你或已被删除。
text404 {"error":"Record not found"}Attaching media (optional)
附加媒体(可选)
Upload each image/video via
(, field ) to get a media id, then pass the ids as
when posting the status. See the docs for the full media contract:
https://docs.joinmastodon.org/methods/media/
POST $MASTODON_BASE_URL/api/v2/mediamultipart/form-datafilemedia_ids[]通过接口上传每张图片/视频(使用格式,字段名为)以获取媒体ID,然后在发布状态时将ID作为参数传入。完整的媒体接口说明请查看文档:https://docs.joinmastodon.org/methods/media/
POST $MASTODON_BASE_URL/api/v2/mediamultipart/form-datafilemedia_ids[]Gotchas
注意事项
- Federated: the API only ever targets (the user's own instance). There is no global endpoint — a token from instance A won't work on instance B.
$MASTODON_BASE_URL - Scopes: reading needs (or
read/read:accounts); posting/deleting needsread:statuses(orwrite). Awrite:statuses(403) means the token lacks a scope."This action is outside the authorized scopes" - Rate limits: Mastodon rate-limits per token; space out bulk posts or you'll
get .
429 - returns HTML in fields like
verify_credentials; the plaintext source lives under thenoteobject.source
- 联邦特性:API仅针对(用户自己的实例)。不存在全局端点——实例A的令牌无法在实例B上使用。
$MASTODON_BASE_URL - 权限范围:读取操作需要(或
read/read:accounts)权限;发布/删除操作需要read:statuses(或write)权限。write:statuses错误(403)表示令牌缺少对应权限。"This action is outside the authorized scopes" - 速率限制:Mastodon针对每个令牌设置了速率限制;批量发布时请间隔发送请求,否则会收到错误。
429 - 返回的
verify_credentials等字段包含HTML内容;纯文本源数据位于note对象下。source
Record the output
记录输出结果
After you successfully publish and obtain the live result URL, call the built-in
tool ONCE so the user can track this deliverable in My Outputs:
publish_artifactpublish_artifact(kind="message", channel="mastodon", title="<title>", url="<the REAL returned URL>", status="delivered")Use the real returned URL — never fabricate one. Call it once per published item,
only after delivery is confirmed; skip it (or use ) if publishing failed.
See .
status="failed"_shared/artifacts.md成功发布并获取到实际结果URL后,调用内置的工具一次,以便用户在我的输出中跟踪该交付成果:
publish_artifactpublish_artifact(kind="message", channel="mastodon", title="<title>", url="<the REAL returned URL>", status="delivered")请使用实际返回的URL——切勿伪造。每个发布项仅调用一次,且仅在确认交付成功后调用;若发布失败,请跳过调用(或使用)。详情请查看。
status="failed"_shared/artifacts.md