Shell脚本中的卷曲命令返回错误{“ Error”:“ bpapigw-300无法授权对资源的访问”

时间:2019-02-05 07:11:08

标签: shell curl command-line scripting using

我正在尝试使用以下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之前是否需要添加任何语法?

2 个答案:

答案 0 :(得分:0)

我看到的是-H "Authorization: Bearer =${TOKEN}"包含一个=符号,该符号不应存在...
应该是:-H "Authorization: Bearer ${TOKEN}"

更多,在命令中,您使用/tmp/curloutput.txt文件,该文件永远不会由脚本创建...

答案 1 :(得分:0)

您使用的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