在shell脚本curl命令返回错误{“错误”:“bpapigw-300不能授权访问资源”

问题描述 投票:0回答:2

我试图用这个shell脚本执行卷曲:

#!/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之前添加任何语法?

shell curl command-line scripting using
2个回答
0
投票

我看到的是,-H "Authorization: Bearer =${TOKEN}"包含了不应该出现的=标志... 它应该是:-H "Authorization: Bearer ${TOKEN}"

更多的,请在命令你使用/tmp/curloutput.txt文件,它永远不会被你的脚本创建...


0
投票

您正在使用的Authorization头不能正常工作。也许语法不Bearer =aAbBcCdDeEfF0123456但对于服务器别的东西上haxsne09运行,也许没有像=建议@MarcoS。或者,您的grep命令可能会返回一个字符太多(流氓报价可能)。

我改写了下面你的代码更易读。你会发现,我:

  • 改变你的匹配组在sed只捕捉到需要的部分,并使用read把它们放在变量。我还使用了-E标志,以避免使用\(\)
  • 删除无用for循环
  • 所有引用变量扩展正确
  • 增加了一些行打破了可读性
  • 猫删除一些临时文件和无用的相关用途

以下是更新后的脚本:

#!/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
© www.soinside.com 2019 - 2024. All rights reserved.