axios oauth请求失败,出现401

时间:2019-04-15 11:58:16

标签: javascript spring-boot axios

我已经能够使用邮递员从我的spring应用程序成功获取JWT令牌,但是我无法使用axios做到这一点。使用axios,我会收到401。

我的代码如下:

axios.post("http://localhost:5000/oauth/token",{
          client_id: 'myClient',
          client_secret: 'superSecretKey',
          scope: 'write',
          grant_type: 'password',
          username: 'myUser',
          password: 'myPassword'
        })

我在邮递员中输入了以下值:

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

由于最后一点,我也尝试过:

axios.post("http://localhost:5000/oauth/token",{
          client_id: 'myClient',
          client_secret: 'superSecretKey',
          scope: 'write',
          grant_type: 'password',
          auth: {
              username: 'myUser',
              password: 'myPassword'
          }
        })

邮递员的日志向我显示了这一点:

Request Headers:
undefined:undefined
Request Body:
grant_type:"password"
username:"myUser"
password:"myPassword"
scope:"write"

但是,在axios的请求中不使用client_id和client_secret仍然会导致401。此外,如果我尝试在没有客户端和机密的情况下检索令牌,则会得到401。

另一个更新: 我在Intellij中安装了一个插件,以向我显示请求。

对于初学者来说,邮递员显示POST方法,而我的Axios请求显示了一种OPTIONS方法,尽管我显然正在使用axios.post。

邮递员-请求标头:

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

邮递员-请求内容:

grant_type=password&username=akroft&password=slivers&scope=write

Axios-请求标头:

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-请求内容:[空]

0 个答案:

没有答案