Mastodon
  • 什么是 Mastodon?
  • 使用 Mastodon
    • 注册帐户
    • 设置你的个人资料
    • 在你的账户发布内容
    • 使用网络功能
    • 处理不想看到的内容
    • 推广你和他人
    • 进行偏好设置
    • 更多设置
    • 在站点外使用 Mastodon
    • 迁移或离开账户
    • 运行你自己的站点
  • 运营 Mastodon
    • 准备你的服务器
    • 从源代码安装
    • 配置你的环境
    • 安装可选功能
      • 对象存储
      • 洋葱服务
      • 验证码
      • 单点登录
    • 配置全文搜索
    • 设置你的新实例
    • 使用管理 CLI
    • 升级到新版本
    • 备份你的服务器
    • 迁移到新机器
    • 扩大你的站点规模
    • 审核操作
    • 故障排除
      • 数据库索引损坏
    • 用户组
  • 开发 Mastodon 应用
    • API 入门指南
    • 使用公开数据
    • 获取客户端应用访问权限
    • 使用帐户登录
    • 库与实现
  • 向 Mastodon 项目做贡献
    • 技术概览
    • 设置开发环境
    • 代码结构
    • 路由
    • 漏洞赏金与责任披露
  • 遵循的标准
    • ActivityPub
    • WebFinger
    • 安全性
    • Microformats
    • OAuth
    • Bearcaps
  • REST API
    • Datetime 格式
    • 指南与最佳实践
    • OAuth 令牌
    • OAuth 作用域
    • 速率限制
  • API 方法
    • apps
      • oauth
      • emails
    • accounts
      • bookmarks
      • favourites
      • mutes
      • blocks
      • domain_blocks
      • filters
      • reports
      • follow_requests
      • endorsements
      • featured_tags
      • preferences
      • followed_tags
      • suggestions
      • tags
    • profile
    • statuses
      • media
      • polls
      • scheduled_statuses
    • timelines
      • conversations
      • lists
      • markers
      • streaming
    • grouped notifications
    • notifications
      • push
    • search
    • instance
      • trends
      • directory
      • custom_emojis
      • announcements
    • admin
      • accounts
      • canonical_email_blocks
      • dimensions
      • domain_allows
      • domain_blocks
      • email_domain_blocks
      • ip_blocks
      • measures
      • reports
      • retention
      • trends
    • proofs
    • oembed
  • API 实体
    • Account
    • AccountWarning
    • Admin::Account
    • Admin::CanonicalEmailBlock
    • Admin::Cohort
    • Admin::Dimension
    • Admin::DomainAllow
    • Admin::DomainBlock
    • Admin::EmailDomainBlock
    • Admin::Ip
    • Admin::IpBlock
    • Admin::Measure
    • Admin::Report
    • Announcement
    • Appeal
    • Application
    • Context
    • Conversation
    • CustomEmoji
    • DomainBlock
    • Error
    • ExtendedDescription
    • FamiliarFollowers
    • FeaturedTag
    • Filter
    • FilterKeyword
    • FilterResult
    • FilterStatus
    • IdentityProof
    • Instance
    • List
    • Marker
    • MediaAttachment
    • Notification
    • NotificationPolicy
    • NotificationRequest
    • Poll
    • Preferences
    • PreviewCard
    • PreviewCardAuthor
    • PrivacyPolicy
    • Reaction
    • Relationship
    • RelationshipSeveranceEvent
    • Report
    • Role
    • Rule
    • ScheduledStatus
    • Search
    • Status
    • StatusEdit
    • StatusSource
    • Suggestion
    • Tag
    • TermsOfService
    • Token
    • Translation
    • V1::Filter
    • V1::Instance
    • V1::NotificationPolicy
    • WebPushSubscription

apps API 方法

注册可用于获取 OAuth 令牌的客户端应用。

    • 创建一个应用
    • 验证你的应用是否有效
    • 另请参考

创建一个应用

POST /api/v1/apps HTTP/1.1

创建一个新的应用以获取 OAuth2 凭据。

在 4.3 之前的 Mastodon 版本中,OAuth 应用可能会在特定条件下被“清理”并从数据库中移除,这意味着你的应用的 client_id 和 client_secret 将不会被 Mastodon 实例识别。
这种自动删除应用的机制已在 Mastodon 4.3 中移除。

对于低于 4.3 的 Mastodon 版本,解决方案之一是注册你的应用,然后立即请求一个 Client Credential 令牌,这将永久确保你的应用始终具有有效的访问令牌,并且不会被移除。
目前,Mastodon 仅支持配置机密客户端,也就是说,你始终会在 CredentialApplication 实体中收到 client_secret 和 client_secret_expires_at 属性。

有关更多信息,请参见:OAuth 2 客户端类型

返回: CredentialApplication
OAuth: 公开
版本历史:
0.0.0 - 添加
2.7.2 - 现在返回 vapid_key
4.3.0 - 弃用 vapid_key,请参见 api/v2/instance
4.3.0 - 添加了对表单数据参数中多个 redirect_uris 的支持
4.3.0 - 添加了 redirect_uris 响应属性
4.3.0 - 弃用 redirect_uri 响应属性,因为若注册了多个 redirect_uris,这可能不是一个 URI,请改用 redirect_uris
4.3.0 - 将实体类型从 Application 更改为 CredentialApplication

请求

请求示例:

POST /api/v1/apps HTTP/1.1
Content-Type: application/json

{
  "client_name": "Test Application",
  "redirect_uris": ["https://app.example/callback", "https://app.example/register"],
  "scopes": "read write push",
  "website": "https://app.example"
}
表单数据参数
client_name
必需 字符串。你的应用的名称。
redirect_uris
必需 字符串或字符串数组。授权后用户应重定向到的位置。要向用户显示授权码,而不是重定向到网页,请在此参数中使用 urn:ietf:wg:oauth:2.0:oob。
scopes
字符串。以空格分隔的作用域列表。若未提供,则默认为 read。有关可能的作用域列表,请参见 OAuth 作用域。
website
字符串。你的应用主页的 URL。

响应

200: OK

将 client_id 和 client_secret 存储在你的缓存中,因为这些将用于获取 OAuth 令牌。

将 client_id 和 client_secret 属性视为密码。我们建议你在存储在缓存中时对其进行加密,以防止凭据泄露。
{
  "id": "563419",
  "name": "Test Application",
  "website": "https://app.example",
  "scopes": ["read", "write", "push"],
  "redirect_uri": "urn:ietf:wg:oauth:2.0:oob",
  "redirect_uris": ["urn:ietf:wg:oauth:2.0:oob"],
  "client_id": "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM",
  "client_secret": "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw"
}

或者使用多个重定向 URI:

{
  "id": "563419",
  "name": "Test Application",
  "website": "https://app.example",
  "scopes": ["read", "write", "push"],
  "redirect_uri": "https://app.example/callback\nhttps://app.example/register",
  "redirect_uris": [
    "https://app.example/callback",
    "https://app.example/register"
  ],
  "client_id": "TWhM-tNSuncnqN7DBJmoyeLnk6K3iJJ71KKXxgL1hPM",
  "client_secret": "ZEaFUFmF0umgBX1qKJDjaU99Q31lDkOU8NutzTOoliw"
}
自 4.3.0 起,以上示例中的 redirect_uri 属性被视为已被弃用,不应使用,而应使用 redirect_uris 属性。
422: Unprocessable entity

若缺少必需的参数或格式不正确,则请求失败。

{
  "error": "Validation failed: Redirect URI must be an absolute URI."
}

验证你的应用是否有效

GET /api/v1/apps/verify_credentials HTTP/1.1

确认应用的 OAuth2 凭据是否有效。

返回: Application
OAuth: 应用令牌
版本历史:
2.0.0 - 添加
2.7.2 - 现在返回 vapid_key
4.3.0 - 弃用 vapid_key,请参见 api/v2/instance
4.3.0 - 移除需使用 read 作用域才能访问此 API 的要求,现在可以使用任何有效的应用令牌
4.3.0 - 添加了 scopes 和 redirect_uris 属性

请求

标头
Authorization
必需 提供此标头以及 Bearer <app_token> 以获得对此 API 方法的访问授权。<app_token> 可以是从 /oauth/token 返回的 client_credential 或 access_token。

响应

200: OK

若 Authorization 标头提供了有效的令牌,你应该看到你的应用作为 Application 实体返回。

{
  "name": "Test Application",
  "website": "https://app.example",
  "scopes": ["read", "write", "push"],
  "redirect_uris": [
    "https://app.example/callback",
    "https://app.example/register"
  ]
}
401: Unauthorized

若 Authorization 标头包含无效的令牌、格式不正确或不存在,则将返回提示授权失败的错误。

{
  "error": "The access token is invalid"
}

另请参考

app/controllers/api/v1/apps_controller.rb
app/controllers/api/v1/apps/credentials_controller.rb

翻译状态: 本文是英文页面 apps API methods 的翻译,最后翻译时间:2025-04-21,点击这里可以查看翻译后页面的改动。

最后更新于 April 21, 2025 · 改进此页面
也可在此找到: English

赞助商

Dotcom-Monitor LoadView Stephen Tures Swayable SponsorMotion

加入Mastodon · 博客 ·

查看源代码 · CC BY-SA 4.0 · 版权信息