2017-09-11 18 views
0

阵营母语JWT-AUTH错误获取API反应本地JWT-AUTH错误获取API

我试图通过使用JWT-AUTH,通过以下方法来获取用户的细节。

login() { 
    alert(this.state.UserName); 
    fetch (Api+''+'/wp-json/jwt-auth/v1/token',{ 
     method: "POST", 
     username: this.state.UserName, 
     password: this.state.Password 
    }).then((response) => response.json()) 
     .then((responseData) =>{ 
      console.log("LoginData:-" + JSON.stringify(responseData)); 
     }).done(); 
    } 
} 

,并收到错误: -

{ 
    "code":"[jwt_auth] empty_username", 
    "message":"<strong>ERROR</strong>: The username field is empty.", 
    "data":{"status":403} 
} 

如果有谁知道请帮助。

回答

0

如果你的API是使用基本身份验证您需要添加用户名和密码的认证头

const base64 = require('base-64'); 
login() { 
    alert(this.state.UserName); 
    fetch (Api+''+'/wp-json/jwt-auth/v1/token',{ 
     method: "POST", 
     headers: { 
      'Authorization', 'Basic ' + base64.encode(this.state.UserName+ ":" + this.state.Password) 
     } 
    }).then((response) => response.json()) 
     .then((responseData) =>{ 
      console.log("LoginData:-" + JSON.stringify(responseData)); 
     }).done(); 
    } 
}