shell - shell脚本中的curl命令返回错误{“错误”:“bapigw-300无法授权访问资源”
问题描述
我正在尝试使用这个 shell 脚本执行 curl:
#!/bin/bash
curl -k -H "Content-Type:application/json" -d '{"username":"admin","password":"adminpw", "tenant":"master"}' https://localhost/tron/api/v1/tokens > /tmp/token.data
grep -Po '{\"token\":\"\K[^ ]...................' /tmp/token.data > /tmp/token
tokendetails=`cat /tmp/token`
for token in $tokendetails
do
TOKEN=`echo $token`
done
userdetails=`cat /tmp/curloutput.txt | sed 's/{"clientInactivityTime"/\n{"clientInactivityTime"/g' | sed 's/\(.*\).\("firstName":[^,]*\)\(.*\)\("lastName":[^,]*\)\(.*\)\("email":[^,]*\)\(.*\)\("username":[^,]*\)\(.*\)/\2,\4,\6,\8/g' | grep username`
for user in $userdetails
do
firstName=`echo $user | sed 's/,/\n/g' | grep firstName | sed 's/.*:"\([^"]*\).*/\1/g'`
lastName=`echo $user | sed 's/,/\n/g' | grep lastName | sed 's/.*:"\([^"]*\).*/\1/g'`
email=`echo $user | sed 's/,/\n/g' | grep email | sed 's/.*:"\([^"]*\).*/\1/g'`
username=`echo $user | sed 's/,/\n/g' | grep username | sed 's/.*:"\([^"]*\).*/\1/g'`
curl -k -X POST "https://haxsne09/tron/api/v1/users" -H "accept: application/json" -H "Authorization: Bearer =${TOKEN}" -H "Content-Type: application/x-www-form-urlencoded" -d "first_name=${firstName}\&last_name=${lastName}\&email=${email}\&password=Tata123^\&username=${username}\&is_active=true"
echo $RESPONSE
done
我收到错误:
{"Error":"bpapigw-300 Cannot authorize access to resource: Could not authorize path for user identifier: Failed to get Roles for identifier: REST operation failed 0 times: '[GET /api/v1/current-user][401] currentUserListUnauthorized \u0026{Detail:Invalid token}'. This user is unauthenticated?"}
在执行之前我需要添加任何语法curl -k -X POST
吗?
解决方案
您使用的Authorization
标头不起作用。也许语法不是Bearer =aAbBcCdDeEfF0123456
运行在服务器上的其他东西haxsne09
,也许没有=
@MarcoS 建议的那样。或者,您的 grep 命令可能返回一个太多字符(可能是一个流氓引号)。
我在下面重写了您的代码以提高可读性。你会注意到我:
- 更改您的匹配组
sed
以仅捕获所需的部分并将它们放入变量中使用read
. 我还使用了-E
标志来避免使用\(
和\)
- 删除了无用的
for
循环 - 正确引用所有变量扩展
- 添加了一些换行符以提高可读性
- 删除了一些临时文件和相关的 cat 无用用途
这是更新的脚本:
#!/bin/bash
curl -k -H 'Content-Type:application/json' -d \
'{"username":"admin","password":"adminpw", "tenant":"master"}' \
https://localhost/tron/api/v1/tokens > /tmp/token.data
token=$(grep -Po '{"token":"\K[^ ]...................' /tmp/token.data)
IFS=, read -r firstName lastName email username < <(
</tmp/curloutput.txt sed 's/{"clientInactivityTime"/\n&/' |
sed -nE 's/.*."firstName":"([^"]*)".*"lastName":"([^"]*)").*"email":"([^"]*).*"username":"([^"]*)".*/\1,\2,\3,\4/p'
)
curl -k -X POST 'https://haxsne09/tron/api/v1/users' -H 'accept: application/json' \
-H "Authorization: Bearer $token" -H "Content-Type: application/x-www-form-urlencoded" -d \
"first_name=$firstName&last_name=$lastName&email=$email&password=Tata123^&username=$username&is_active=true"
echo
推荐阅读
- python - 我应该在 keras 中使用什么损失函数和度量来进行多标签分类?
- java - 无法在 Android Studio 中读取 JSON
- c++ - ESP32 尝试重新连接到网络断开时没有停止循环(Arduino)
- python - 使用 AES-GCM 加密不适合 RAM 的大文件
- python - 堆叠布局导致对齐顶部问题
- google-bigquery - bigquery 预订是自动的吗?
- c# - 如何在静态类中绑定属性?
- reactjs - 我的反应钩子在我的反应组件中使用时失败,该组件深深嵌入到我的反应应用程序中
- node.js - 程序在继续流程之前不等待异步功能
- java - 不可变 @Value.Immutable 公共最终字段(而不是 getter)