首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >解析错误:第1行第8列的无效数字文字

解析错误:第1行第8列的无效数字文字
EN

Stack Overflow用户
提问于 2020-03-12 04:15:14
回答 1查看 2.4K关注 0票数 0

我正试图通过使用curl获得一个令牌到某个站点。它看起来是正确的,因为我必须等待一些响应,但是在反序列化过程中有一些事情是在反序列化期间,因为我总是得到错误:解析错误:在第1行第8列无效的数字文字。

这就是脚本的样子:

代码语言:javascript
代码运行次数:0
运行
复制
TOKEN=$(curl --request POST \
--url 'https://${DOMAIN_NAME}/getmy/token' \
--header 'content-type: application/json' \
--data '{"grant_type":"password", "username":"${USER_EMAIL}",
"password":"${USER_PASSWORD}",
"audience":"https://localhost:8443/my-composite-service", "scope":"openid
email test:read test:write", "client_id": "${CLIENT_ID}",
"client_secret": "${CLIENT_SECRET}"}' -s | jq -r .access_token)

是因为jq吗?

更重要的是,我确信存在env变量,即使使用硬编码的值,也会抛出相同的错误。

提前谢谢你

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-12 09:30:26

一些提示:

instead.

  • Structure

  • 并不是把所有东西都放在一行中,而是用函数使其成为可读的JSON。

  • 做错误处理。

  • 使用Bash的调试JSON而不是使用字符串连接的JSON,而是使用JQ,因为只有JQ正确引用JSON数据。密码可能包含引用字符.

举个例子:

代码语言:javascript
代码运行次数:0
运行
复制
set -eu
set -x

USER_EMAIL="user@domain.org"
USER_PASSWORD="password"
CLIENT_ID="id"
CLIENT_SECRET="secret"
DOMAIN_NAME="domain.org"

data()
{
  local template='
{
  "grant_type":    "password",
  "username":      $username,
  "password":      $password,
  "audience":      "https://localhost:8443/my-composite-service",
  "scope":         "openid email test:read test:write",
  "client_id":     $client_id,
  "client_secret": $client_secret
}'

  if jq <<<null -c \
        --arg username      "${USER_EMAIL}" \
        --arg password      "${USER_PASSWORD}" \
        --arg client_id     "${CLIENT_ID}" \
        --arg client_secret "${CLIENT_SECRET}" \
        "$template"
  then
    return
  else
    printf "ERROR: Can not format request data." >&2
    exit 1
  fi
}

post()
{
  if curl --request POST \
          --url     'https://${DOMAIN_NAME}/getmy/token' \
          --header  'content-type: application/json' \
          --data    "$1" \
          -s
  then
    return
  else
    printf "ERROR: Can not send post request." >&2
    exit 1
  fi
}

token()
{
  if jq -r .access_token
  then
    return
  else
    printf "ERROR: Can not parse JSON response." >&2
    exit 1
  fi
}

TOKEN="$(post "$(data)" | token)"
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60647553

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档