watson 机器学习 api - 令牌刷新 400 错误

watson machine learning api - token refresh 400 error

我已经使用 GET /v3/identity/token API 成功生成了一个令牌。我现在希望能够利用 PUT API 来保持令牌处于活动状态。

我正在尝试这个 curl 命令:

curl -X PUT --header 'Content-Type: application/json' --header 'Accept: application/json' -d '<token-value>' 'https://ibm-watson-ml.mybluemix.net/v3/identity/token' -v -i --basic --user <username>:<password>

我收到 400 错误,指出:

For request 'PUT /v3/identity/token' [Invalid Json: Unexpected character ('-' (code 45)) in numeric value: expected digit (0-9) to follow minus sign, for valid numeric value at [Source: akka.util.ByteIterator$ByteArrayIterator$$anon@18bd12ef; line: 1, column: 3]]

get 请求返回的令牌中包含破折号以及其他非字母数字值。

get请求的token是否需要解析?我错过了什么?

您需要将内容类型设置为 application/json。但是 -d 发送了 Content-Type application/x-www-form-urlencodedmaybe 在 IBM 端不被接受。

但是,您的 JSON(令牌)格式似乎不正确。

令牌值需要采用以下格式 (JSON):

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.TJVA95OrM7E2cBab30RMHrHDcEfxjoYZgeFONFh7HgQ"
}

并且您需要按照示例正确发送格式:

curl -H 'Content-Type: application/json' -X PUT \
-d '{"token":"yourToken"}' \
https://ibm-watson-ml.mybluemix.net/v3/identity/token

见官方reference.