【发布时间】:2018-04-15 10:47:43
【问题描述】:
类似于this question 我正在向服务器发送以下 POST:
content-type: application/x-www-form-urlencoded
authorization: Basic dGVzdGp3dGNsaWVudGlkOlhZN2ttem9OemwxMDA=
accept: */*
使用有效载荷:
{"username"="test", "password": "pw", "email": "test@example.com"}
但是,我仍然得到了
{
"error": "invalid_request",
"error_description": "Missing grant type"
}
作为来自服务器的响应。知道为什么这不起作用吗?
请注意,如果我使用curl,则请求有效:
$ curl testjwtclientid:XY7kmzoNzl100@localhost:8080/oauth/token -d grant_type=password -d username=john.doe -d password=pw -v
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 8080 (#0)
* Server auth using Basic with user 'testjwtclientid'
> POST /oauth/token HTTP/1.1
> Host: localhost:8080
> Authorization: Basic dGVzdGp3dGNsaWVudGlkOlhZN2ttem9OemwxMDA=
> User-Agent: curl/7.47.0
> Accept: */*
> Content-Length: 49
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 49 out of 49 bytes
< HTTP/1.1 200
< X-Content-Type-Options: nosniff
< X-XSS-Protection: 1; mode=block
< Cache-Control: no-cache, no-store, max-age=0, must-revalidate
< Pragma: no-cache
< Expires: 0
< X-Frame-Options: DENY
< Cache-Control: no-store
< Pragma: no-cache
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Thu, 02 Nov 2017 19:21:29 GMT
<
* Connection #0 to host localhost left intact
{"access_token":"<an-ugly-long-token>","token_type":"bearer","expires_in":43199,"scope":"read write","jti":"80e2b6af-d999-4fb6-a4cd-5e6ab9c3fcaa"}
【问题讨论】:
-
您的有效负载应该类似于 grant_type=password&username=john.doe&password=pw,而您将其作为 JSON 传递。您可以检查 stackoverflow.com/questions/38165131/… 的 JSON 有效负载
-
我将我的评论作为答案,如果它解决了您的问题,请接受。