Bash 无法读取变量
Bash unable to read variable
我正在使用 Azure Log Analytics 获取一些信息,但首先我需要从 1 个命令获取 OAuth 令牌并将其传递到下一个命令。我有以下 Curl 命令,我自己测试过这些命令(复制粘贴下一个输入的输出),但是我想将 OAuth 令牌输出作为自动化任务的变量传递,但由于某种原因它不能将变量读入下一个命令。
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq .access_token)
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H 'Authorization: Bearer $token' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'
不幸的是,当我 运行 这个命令时,它会回应说需要一个令牌。
{"error":{"message":"Valid authentication was not provided","code":"AuthorizationRequiredError"}}
但是,如果我回显 $token
变量,它表明它已保存
beefcake@ubuntu:~$ echo $token
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1...."
正如我所说,如果我删除 token=$(..)
并且只是将输出 copy/paste 删除到下一个输入中,这些命令就可以正常工作。知道为什么这对自动化不起作用吗?
@Aserre 的心态是正确的。结果是 jq 从字符串中复制了引号 " "
,而不记名标记需要 none。因此我的第一个命令应该是这样的:
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq -r .access_token)
请注意最后一行 -r
命令用于删除双引号。其中显示了以下回声:
beefcake@ubuntu:~$ echo $token
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIs....
注意从 echo 中删除的 " "
。除此之外,我还必须更改下一个命令,将 'Authorization: Bearer $token'
替换为 "Authorization: Bearer $token"
:
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H "Authorization: Bearer $token" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'
我正在使用 Azure Log Analytics 获取一些信息,但首先我需要从 1 个命令获取 OAuth 令牌并将其传递到下一个命令。我有以下 Curl 命令,我自己测试过这些命令(复制粘贴下一个输入的输出),但是我想将 OAuth 令牌输出作为自动化任务的变量传递,但由于某种原因它不能将变量读入下一个命令。
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq .access_token)
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H 'Authorization: Bearer $token' \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'
不幸的是,当我 运行 这个命令时,它会回应说需要一个令牌。
{"error":{"message":"Valid authentication was not provided","code":"AuthorizationRequiredError"}}
但是,如果我回显 $token
变量,它表明它已保存
beefcake@ubuntu:~$ echo $token
"eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1...."
正如我所说,如果我删除 token=$(..)
并且只是将输出 copy/paste 删除到下一个输入中,这些命令就可以正常工作。知道为什么这对自动化不起作用吗?
@Aserre 的心态是正确的。结果是 jq 从字符串中复制了引号 " "
,而不记名标记需要 none。因此我的第一个命令应该是这样的:
token=$(curl -X POST \
https://login.microsoftonline.com/{{subscriptionID}}/oauth2/token \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d 'grant_type=client_credentials&client_id={{clientID}}&client_secret={{clientSECRET}}&resource=https%3A%2F%2Fapi.loganalytics.io' \
| jq -r .access_token)
请注意最后一行 -r
命令用于删除双引号。其中显示了以下回声:
beefcake@ubuntu:~$ echo $token
eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIs....
注意从 echo 中删除的 " "
。除此之外,我还必须更改下一个命令,将 'Authorization: Bearer $token'
替换为 "Authorization: Bearer $token"
:
curl -X POST \
https://api.loganalytics.io/v1/workspaces/{{workspaceID}}/query \
-H "Authorization: Bearer $token" \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/json' \
-d '{ "query": "AzureActivity | summarize count() by Category" }'