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

调用Tumblr API /posts端点时返回Bad Request错误求助

解决Tumblr API多图博文发布的错误问题

原代码的核心问题

  • 参数字段误用:你使用了content数组来定义图片,但Tumblr的Photo类型博文需要用photos字段来配置多张图片及对应说明,content仅适用于富文本类型的博文。
  • 请求方式适配问题:结合OAuth1签名时,用表单数据(data参数)发送请求会更稳定,避免JSON请求可能导致的签名验证失败。

修正后的代码

import requests
from requests_oauthlib import OAuth1

# 替换为你的真实密钥信息
consumer_key = "0000000000000000000000000000000000000"
consumer_secret = "0000000000000000000000000000000000000"
token = "0000000000000000000000000000000000000"
token_secret = "0000000000000000000000000000000000000"

oauth = OAuth1(
    consumer_key,
    client_secret=consumer_secret,
    resource_owner_key=token,
    resource_owner_secret=token_secret,
    signature_method='HMAC-SHA1'
)

url = 'https://api.tumblr.com/v2/blog/aisamarsha-blog.tumblr.com/posts'

# 修正参数结构:用photos替代content,每个元素用source指定图片URL
params = {
    'type': 'photo',
    'state': 'published',
    'tags': 'tag1, tag2, tag3',
    'photos': [
        {
            'source': 'http://69.media.tumblr.com/b06fe71cc4ab47e93749df060ff54a90/tumblr_nshp8oVOnV1rg0s9xo1_250.jpg',
            'caption': 'This is the caption for image1'
        },
        {
            'source': 'http://69.media.tumblr.com/b06fe71cc4ab47e93749df060ff54a90/tumblr_nshp8oVOnV1rg0s9xo1_250.jpg',
            'caption': 'This is the caption for image2'
        },
        {
            'source': 'http://69.media.tumblr.com/b06fe71cc4ab47e93749df060ff54a90/tumblr_nshp8oVOnV1rg0s9xo1_1280.jpg',
            'caption': 'This is the caption for image3'
        }
    ]
}

# 用data参数发送表单数据,适配OAuth1签名和Tumblr API要求
response = requests.post(url, auth=oauth, data=params)

print(response.json())

额外注意事项

  • 确认你的OAuth密钥具备发布博文的权限(检查Tumblr开发者后台的应用权限设置)。
  • 图片URL必须是公开可访问的,Tumblr会抓取图片并存储到自身服务器,本地图片需先上传至公开存储服务。
  • 若仍报错,可打印response.status_code和response.text查看具体错误信息,比如权限不足、URL无效等。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 02:05:44