I've been able to successfully get a JWT token from my spring application using postman, but I'm not able to do so with axios. With axios I'm receiving a 401.
My code is as follows:
axios.post("http://localhost:5000/oauth/token",{
client_id: 'myClient',
client_secret: 'superSecretKey',
scope: 'write',
grant_type: 'password',
username: 'myUser',
password: 'myPassword'
})
I'm putting the following values in Postman:
Token Name: My Token
Grant Type: Password Credentials
Access Token URL: http://localhost:5000/oauth/token
Username: myUser
Password: myPassword
Client ID: myClient
Client Secret: superSecretKey
Scope: write
Client Authentication: Send as Basic Auth header
Because of that last bit, I have also tried:
axios.post("http://localhost:5000/oauth/token",{
client_id: 'myClient',
client_secret: 'superSecretKey',
scope: 'write',
grant_type: 'password',
auth: {
username: 'myUser',
password: 'myPassword'
}
})
The log in Postman shows me this:
Request Headers:
undefined:undefined
Request Body:
grant_type:"password"
username:"myUser"
password:"myPassword"
scope:"write"
However, not using client_id and client_secret in my request from axios still results in a 401. Also, I get a 401 if I attempt to retrieve the token without client and secret.
Another update: I installed a plugin in Intellij to show me requests as they come in.
For starters, Postman shows method of POST, while my Axios request shows a method of OPTIONS, despite that I'm clearly using axios.post.
Postman - Request Headers:
Authorization: Basic SomeEncodedLookingStringThatsNotMySecret
content-length: 64
Accept: */*
User-Agent: PostmanRuntime/7.6.0
Connection: keep-alive
Host: localhost:5000
accept-encoding: gzip, deflate
Content-Type: application/x-www-form-urlencoded
Postman - Request Content:
grant_type=password&username=akroft&password=slivers&scope=write
Axios - Request Headers:
Origin: http://localhost:8080
Accept: */*
Connection: keep-alive
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86
Safari/537.36
Referer: http://localhost:8080/login
Host: localhost:5000
Pragma: no-cache
Accept-Encoding: gzip, deflate, br
Cache-Control: no-cache
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type
Accept-Language: en-US,en;q=0.9
Content-Length: 0
Axios - Request Content: [empty]