我如何通过授权头使用cURL?(可在/usr/bin/curl中执行)。
当前回答
从curl 7.61.0开始,您可以使用——oauth2-bearer <token>选项来设置正确的承载授权标头。
其他回答
一个简单的例子是使用授权转换为base64的参数
curl -XPOST 'http://exemplo.com/webhooks?Authorization=Basic%20dGVzdDoxMjM0NTYK'
这招对我很管用:
curl -H "Authorization: Bearer xxxxxxxxxxxxxx" https://www.example.com/
注意,当你使用: 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
如果在调用时没有令牌,则必须进行两次调用,一次获取令牌,另一次从响应中提取令牌,请注意
Grep令牌| cut -d, -f1 | cut -d\" -f4
因为它是处理从响应中提取令牌的部分。
echo "Getting token response and extracting token"
def token = sh (returnStdout: true, script: """
curl -S -i -k -X POST https://www.example.com/getToken -H \"Content-Type: application/json\" -H \"Accept: application/json\" -d @requestFile.json | grep token | cut -d, -f1 | cut -d\\" -f4
""").split()
提取令牌后,您可以使用令牌进行后续调用,如下所示。
echo "Token : ${token[-1]}"
echo "Making calls using token..."
curl -S -i -k -H "Accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer ${token[-1]}" https://www.example.com/api/resources
推荐文章
- 在Bash中将输出赋给变量
- HTTPS和SSL3_GET_SERVER_CERTIFICATE:证书验证失败,CA is OK
- 如何在Node.js内进行远程REST调用?旋度吗?
- 如何为已安装的Ubuntu LAMP堆栈启用cURL ?
- 为什么cURL返回错误“(23)Failed writing body”?
- 如何POST JSON数据与PHP卷曲?
- 使用curl在PHP中获取HTTP代码
- SSL证书错误:无法获得本地颁发者证书
- 从URL执行bash脚本
- PHP中的cURL是什么?
- 对bash脚本函数中定义的变量使用curl POST
- 使用curl POST multipart/form-data的正确方法是什么?
- PHP -调试Curl
- (1) libcurl不支持或禁用https协议
- 在尝试访问HTTPS url时,如何使用cURL处理证书?