缺少表单参数:grant_type IONIC CAPACITOR

问题描述 投票:0回答:1

我正在尝试调用 Keycloak,但控制台给了我这个错误:

缺少表单参数:GRANT_TYPE

这是我的代码:

post() {
    debugger;
    let url = "http://172.20.10.3:8080/realms/myrealm/protocol/openid-connect/token";
    CapacitorHttp.request({
      method:"POST",
      url:url,
      headers:{'Content-Type': 'application/x-www-form-urlencoded'},
      data:{'username':this.username,
            'password':this.password,
            'grant_type':'password',
            'client_id':'myclient',
            'client_secret':'fk8oMLpTwdv98zBuhoWd11QWyZlojYsV',
            'claim':'normal_user'
          }
    }).then(res => {
      console.log(res.data);
      this.dataReceived = JSON.stringify(res.data.access_token);
    })
  };

有什么问题吗?

angular ionic-framework ionic3 keycloak
1个回答
0
投票

问题是您发送的数据不是

application/x-www-form-urlencoded
内容类型,请参阅 https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST#example

所以你的请求应该是这样的

post() {
  debugger;
  let url = "http://172.20.10.3:8080/realms/myrealm/protocol/openid-connect/token";
CapacitorHttp.request({
    method:"POST",
    url:url,
    headers:{'Content-Type': 'application/x-www-form-urlencoded'},
    data:'username=' + this.username
        + '&password=' + this.password
        + '&grant_type=' + 'password'
        + '&client_id=' + 'myclient'
        + '&client_secret=' + 'fk8oMLpTwdv98zBuhoWd11QWyZlojYsV'
        + '&claim=' + 'normal_user'
      }
  }).then(res => {
    console.log(res.data);
    this.dataReceived = JSON.stringify(res.data.access_token);
  })
};
© www.soinside.com 2019 - 2024. All rights reserved.