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

无需用户令牌调用Mastodon V2搜索API遇400错误求助

问题描述

我尝试在Python 3中调用mastodon.social的Mastodon V2搜索API,搜索特定账号使用的特定标签,代码如下:

import requests

url = 'https://mastodon.social/api/v2/search'
params = {'q': '<hashtag to search for>', 'type':'hashtags', 'account_id':'<my account id>'}

response = requests.get(url, data=params)

print(response)

按官方说明,4.0.0版本起该API无需用户令牌(仅不能用resolve或offset参数),而mastodon.social至少运行4.1.4版本,理论上可行,但始终返回Bad Request <Response [400]>,想问哪里操作错了?

问题修复方案
  • GET请求参数传递方式错误
    GET请求的参数必须通过params参数传递,你用了data参数——这个参数是给POST请求传表单数据用的,用在GET里会导致参数无法被服务器正确解析,直接触发400错误。
  • 参数组合可能不被API支持
    当你指定type='hashtags'的同时搭配account_id,这个组合可能不在V2搜索API的允许范围内。官方文档里,account_id参数主要用于过滤账号或状态的搜索结果,针对标签搜索时该参数可能无效,甚至会被服务器判定为非法参数导致400。

修复后的代码示例:

import requests

url = 'https://mastodon.social/api/v2/search'
# 搜索标签时q直接填标签名(无需带#),同时去掉type参数,或调整为搜索状态再筛选标签
params = {'q': 'your-target-hashtag', 'account_id': 'your-account-id'}

response = requests.get(url, params=params)

# 打印状态码和响应内容方便排查
print(response.status_code)
print(response.json())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 05:12:17