2017-09-11 47 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(); 
    } 
}