gpt4 book ai didi

curl - 为什么获取 Twitter API 的仅应用程序身份验证的不记名 token 的 POST 请求会返回 400 Bad 请求?

转载 作者:行者123 更新时间:2023-12-02 00:10:29 25 4
gpt4 key购买 nike

我正在尝试按照以下说明获取不记名 token 以进行 Twitter API 的仅应用程序身份验证:

https://dev.twitter.com/oauth/application-only

但是,每次我使用curl发出此处描述的请求时,我都会收到返回的“400 Bad request”状态和空响应正文。谁能看出我做错了什么吗?

我正在尝试使用 curl 进行此操作,认为这样可以更轻松地发出正确的请求。第一:

$ export CONSUMER_KEY="[...]"
$ export CONSUMER_SECRET="[...]"

显然我已经省略了那里的值,但这些是我从 https://apps.twitter.com/ 获得的值在我创建的应用程序的“ key 和访问 token ”选项卡上 - 它们来自此处的两个编辑部分:

redacted screenshot of apps.twitter.com ID keys and access tokens tab

然后我通过以下方式提出请求:

curl --trace-ascii curl-trace \
-X POST \
--data 'grant_type=client_credentials' \
-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \
-H "User-Agent: YNR Twitter ID mapper v0.0.1" \
-H "Authorization: Basic $(echo -n "$CONSUMER_KEY:$CONSUMER_SECRET" | base64)" \
'https://api.twitter.com/oauth2/token'

然而,即使发送到服务器的内容看起来完全符合文档的要求,这仍然会返回 400 Bad 请求和空正文。为了说明这一点,命令的 curl-trace 文件的相关输出是:

== Info: Hostname was NOT found in DNS cache
== Info: Trying 104.244.42.194...
== Info: Connected to api.twitter.com (104.244.42.194) port 443 (#0)
[... elided SSL connection details ...]
0000: POST /oauth2/token HTTP/1.1
001d: Host: api.twitter.com
0034: Accept: */*
0041: Content-Type: application/x-www-form-urlencoded;charset=UTF-8
0080: User-Agent: YNR Twitter ID mapper v0.0.1
00aa: Authorization: Basic [...]
00ea: [...]
012a: Content-Length: 29
013e:
=> Send data, 29 bytes (0x1d)
0000: grant_type=client_credentials
== Info: upload completely sent off: 29 out of 29 bytes
== Info: HTTP 1.0, assume close after body
<= Recv header, 26 bytes (0x1a)
0000: HTTP/1.0 400 Bad Request
<= Recv header, 19 bytes (0x13)
0000: content-length: 0
<= Recv header, 37 bytes (0x25)
0000: date: Sun, 24 Apr 2016 12:55:59 GMT
<= Recv header, 15 bytes (0xf)
0000: server: tsa_f
<= Recv header, 53 bytes (0x35)
0000: x-connection-hash: [...]
<= Recv header, 2 bytes (0x2)
0000:
== Info: Closing connection 0
== Info: SSLv3, TLS alert, Client hello (1):
=> Send SSL data, 2 bytes (0x2)
0000: ..

我很困惑为什么这不起作用!

最佳答案

(好吧,就像经常发生的那样,我在发布问题之前就找出了问题所在,但答案可能对其他人有帮助,所以我还是将其发布在这里。)

什么the Twitter docs在您对 $CONSUMER_KEY:$CONSUMER_SECRET 进行 base64 编码并在 Authorization header 中发送它的部分中,正在描述,只是 HTTP basic authentication .

正如维基百科页面所说:

The resulting string is encoded using the RFC2045-MIME variant of Base64, except not limited to 76 char/line.

(我的重点)。当我构造字符串时:

echo -n "$CONSUMER_KEY:$CONSUMER_SECRET" | base64

...在输出中包含一个换行符 (0a) 以打破长行,正如该引用所解释的那样,在对 HTTP 基本访问身份验证的字符串进行编码时,您不需要这样做。您可以使用 -w 0 抑制换行,如下所示:

echo -n "$CONSUMER_KEY:$CONSUMER_SECRET" | base64 -w 0

所以我尝试的命令的工作版本是:

curl --trace-ascii curl-trace \
-X POST \
--data 'grant_type=client_credentials' \
-H "Content-Type: application/x-www-form-urlencoded;charset=UTF-8" \
-H "User-Agent: YNR Twitter ID mapper v0.0.1" \
-H "Authorization: Basic $(echo -n "$CONSUMER_KEY:$CONSUMER_SECRET" | base64 -w 0)" \
--compressed \
'https://api.twitter.com/oauth2/token'

更简单的版本

使用 -u 选项让 curl 为您进行 HTTP 基本身份验证会更容易,而不是乱搞 base64你自己。对我来说最简单的 curl 命令版本是:

curl -u "$CONSUMER_KEY:$CONSUMER_SECRET" \
--compressed \
--data 'grant_type=client_credentials' \
'https://api.twitter.com/oauth2/token'

(感谢 Hans Zthis question 的评论,让我走上了正轨!)

关于curl - 为什么获取 Twitter API 的仅应用程序身份验证的不记名 token 的 POST 请求会返回 400 Bad 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36823839/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com