通过cURL获取访问令牌

通过cURL获取访问令牌,curl,keycloak,Curl,Keycloak,简单问题: 为什么下面的代码可以工作。。。(它返回的访问令牌刚刚好) 这个没有 curl -d '{"grant_type":"client_credentials","client_secret":"8f6a6e73-66ca-4f8f-1234-ab909147f1cf","client_id":"synchronization_tool"}' http://localhost:8080/auth/realms/master/protocol/openid-connect/token -H

简单问题:

为什么下面的代码可以工作。。。(它返回的访问令牌刚刚好)

这个没有

curl -d  '{"grant_type":"client_credentials","client_secret":"8f6a6e73-66ca-4f8f-1234-ab909147f1cf","client_id":"synchronization_tool"}' http://localhost:8080/auth/realms/master/protocol/openid-connect/token -H "Content-Type: application/json"
它给了我:

"error":"invalid_request","error_description":"Missing form parameter: grant_type"}

它们不是应该是两个完全相似的请求吗?

好吧,这些cURL查询似乎不是相似的

此外,端点不理解JSON,它只接受x-www-form-urlencoded查询

curl -d 'client_id=xxx' -d 'username=xxx' -d 'password=xxx' -d 'grant_type=password' 'http://localhost:8080/auth/realms/YOUR_REALM_NAME/protocol/openid-connect/token' | python -m json.tool

这对我来说很有效,它将为您提供
访问令牌
会话令牌

这是您第二次请求中的输入错误吗?json开头缺少一个引号。这两个请求我都得到了404。@delephin谢谢你指出这一点。不幸的是,这是一个打字错误:(关于404,我想这很正常,因为它不是一个真正的服务器名。我会编辑这个问题以避免混淆。谢谢!!!在你做了更改后,第二个req工作了吗?我有点困惑。展开
这一个不
。你有什么错误吗?@delephin没有,当我从我的终端复制并粘贴到SOyou可能也需要d
-d'client\u secret=您的客户\u secret'
curl -d 'client_id=xxx' -d 'username=xxx' -d 'password=xxx' -d 'grant_type=password' 'http://localhost:8080/auth/realms/YOUR_REALM_NAME/protocol/openid-connect/token' | python -m json.tool