我如何通过授权头使用cURL?(可在/usr/bin/curl中执行)。


当前回答

FWIW,在Mac OS上,我发现我需要用引号包围目标url时,它包含查询参数,例如,

curl -H "Authorization: Token xxxxxxxxxxxxxx" "https://www.example.com/?param=myparam"

其他回答

无记名代币看起来是这样的:

curl -H "Authorization: Bearer <ACCESS_TOKEN>" http://www.example.com

(对于那些正在寻找php-curl答案的人)

$service_url = 'https://example.com/something/something.json';
$curl = curl_init($service_url);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); //Your credentials goes here
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $curl_post_data);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); //IMP if the url has https and you don't want to verify source certificate

$curl_response = curl_exec($curl);
$response = json_decode($curl_response);
curl_close($curl);

var_dump($response);

一个简单的例子是使用授权转换为base64的参数

curl -XPOST 'http://exemplo.com/webhooks?Authorization=Basic%20dGVzdDoxMjM0NTYK'

注意,当你使用: curl -H "授权:token_str" http://www.example.com

token_str和Authorization必须用空格隔开,否则服务器端将无法获得HTTP_AUTHORIZATION环境。

只是添加,所以你不需要点击:

curl --user name:password http://www.example.com

或者如果你试图为OAuth 2发送身份验证:

curl -H "Authorization: OAuth <ACCESS_TOKEN>" http://www.example.com