我想和Nim一起使用twitter api。 但是,我无法解决错误。
{“errors”:[{“code”:85,“message”:“列表验证失败:列表名称不能为空。”}]}
我成功认证。
使用twitter API或者我犯了一个错误 使用Nim库oauth1, 发送Post方法体?
import tables, oauth1, strutils, httpclient, json
proc parseResponseBody(body: string): Table[string, string] =
let responses = body.split("&")
result = initTable[string, string]()
for res in responses:
let r = res.split("=")
result[r[0]] = r[1]
proc getRequestToken(consumerKey, consumerKeySecret: string): Table[string, string] =
let response = getOAuth1RequestToken(
"https://api.twitter.com/oauth/request_token",
consumerKey,
consumerKeySecret,
isIncludeVersionToHeader = true)
if response.status == "200 OK":
return parseResponseBody(response.body)
else:
assert(false, response.body)
proc getAccessToken(consumerKey, consumerKeySecret, requestToken, requestTokenSecret, verifier: string): Table[string, string] =
let response = getOAuth1AccessToken(
"https://api.twitter.com/oauth/access_token",
consumerKey,
consumerKeySecret,
requestToken,
requestTokenSecret,
verifier,
isIncludeVersionToHeader = true)
if response.status == "200 OK":
return parseResponseBody(response.body)
else:
assert(false, response.body)
let
consumerKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
consumerKeySecret = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
let requestToken = getRequestToken(consumerKey, consumerKeySecret)
echo getAuthorizeUrl("https://api.twitter.com/oauth/authorize", requestToken["oauth_token"])
let verifier = readLine(stdin)
let accessTokens = getAccessToken(
consumerKey,
consumerKeySecret,
requestToken["oauth_token"],
requestToken["oauth_token_secret"],
verifier)
let param = %*{"name": "chage","mode": "private","description": "description"}
let response = oauth1Request(
"https://api.twitter.com/1.1/lists/create.json",
consumerKey,
consumerKeySecret,
accessTokens["oauth_token"],
accessTokens["oauth_token_secret"],
httpMethod = HttpPost,
body = $param
)
echo response.body
答案 0 :(得分:3)
查看the documentation的Twitter API似乎需要以查询参数的形式输入,而不是JSON正文。这意味着您不需要创建param JSON对象,而是创建参数字符串。这可以通过简单的连接来完成,但请确保使用以下内容转义URI字符:https://nim-lang.org/docs/uri.html#encodeUrl,string