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

使用Nim调用Twitter API遇错误码85,请求排查问题原因

解决Nim调用Twitter API创建列表时的"列表名称不能为空"错误

首先,这个错误(code:85)是Twitter API明确返回的,说明你的请求中没有正确传递name参数,或者参数值为空。既然身份认证已经成功,问题大概率出在POST请求体的构建或传递方式上,和Nim的oauth1库对表单数据的处理有关。

可能的问题点

  • 你可能拼写错了参数名(比如用了title而不是Twitter API要求的name)
  • 请求体没有使用application/x-www-form-urlencoded格式(Twitter创建列表的接口要求这种格式,不是JSON)
  • oauth1库发送POST请求时,没有正确将表单数据附加到请求中

修复方案

下面是正确构建请求并调用API的示例代码,你可以对照调整:

import tables, oauth1, strutils, httpclient, json

proc createTwitterList(consumerKey, consumerSecret, accessToken, accessTokenSecret: string, listName: string): JsonNode =
  let client = newHttpClient()
  defer: client.close()

  # 1. 构建正确的表单参数,必须包含非空的`name`字段
  let formParams = initTable[string, string]()
  formParams["name"] = listName  # 确保这个值不为空字符串
  formParams["description"] = "Created via Nim"  # 可选参数
  formParams["mode"] = "public"  # 可选,public或private

  # 2. 将表单参数转换为urlencoded格式
  let requestBody = formParams.toQuery()

  # 3. 配置OAuth1客户端
  let oauthClient = newOAuth1Client(
    consumerKey = consumerKey,
    consumerSecret = consumerSecret,
    accessToken = accessToken,
    accessTokenSecret = accessTokenSecret
  )

  # 4. 设置请求头,指定Content-Type
  client.headers["Content-Type"] = "application/x-www-form-urlencoded"

  # 5. 发送POST请求,注意要传递requestBody,并且让oauth1库签名请求体
  let response = oauthClient.post(
    client = client,
    url = "https://api.twitter.com/1.1/lists/create.json",
    body = requestBody
  )

  # 解析响应
  if response.status == Http200:
    result = parseJson(response.body)
  else:
    echo "Error: ", response.status, " - ", response.body
    result = newJNull()

# 使用示例
let myList = createTwitterList(
  "YOUR_CONSUMER_KEY",
  "YOUR_CONSUMER_SECRET",
  "YOUR_ACCESS_TOKEN",
  "YOUR_ACCESS_TOKEN_SECRET",
  "My Nim Test List"  # 这里的名称不能为空!
)
echo myList

关键注意事项

  • 一定要确保name参数的值非空,不能是""或者空白字符串
  • 必须使用application/x-www-form-urlencoded格式传递参数,不要用JSON格式(Twitter的这个接口不接受JSON请求体)
  • 确认oauth1库在签名时包含了POST的请求体数据——有些OAuth1库默认只签名URL,需要手动指定包含请求体(上面的示例中oauthClient.post方法应该已经处理了这个,但如果你的库版本不同,可能需要额外配置)

如果调整后还是有问题,可以打印出你实际发送的requestBody内容,确认name参数确实存在且有值,这样就能快速定位问题了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 07:03:11