我正在尝试实现oauth2登录令牌来响应js,但出现错误

时间:2019-10-08 10:50:15

标签: reactjs oauth-2.0 django-rest-framework

我在Django-rest框架中创建了oauth2登录名,我尝试在react js中实现此登录名,但是我一直都遇到错误,因此我需要帮助来解决此问题。我也使用了爷爷和客户ID,但仍然出现错误 Here is the image of error

 var _client_id = "hLloV1YL1XXHmbxst23GU72nAyLi8rCjMhr6cKVn";
    var _grant_type = "password";

    class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
          username: "",
          password: ""
        };
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
      }

      handleChange(event) {
        this.setState({
          [event.target.name]: event.target.value
        });
        console.log(this.state);
      }
      handleSubmit(event) {
        // const { username, password } = params;
        event.preventDefault();
        // let data =
        //   "grant_type=" +
        //   _grant_type +
        //   "&username=" +
        //   username +
        //   "&password=" +
        //   password +
        //   "&client_id=" +
        //   _client_id;
        const request = new Request("http://localhost:8001/api/1/oauth/token/", {
          method: "POST",
          headers: new Headers({
            "Content-Type": "application/x-www-form-urlencoded",
            Accept: "application/json",
            Authorization:
              "Bearer" +
              btoa("someclientid:hLloV1YL1XXHmbxst23GU72nAyLi8rCjMhr6cKVn")
          }),
          body: JSON.stringify({
            username: this.state.username,
            password: this.state.password,
            grant_type: _grant_type,
            client_id: _client_id
          })
        });
        return fetch(request)
          .then(response => {
            if (response.status < 200 || response.status >= 300) {
              throw new Error(response.statusText);
            }
            return response.json();
          })
          .then(({ access_token }) => {
            localStorage.setItem("token", access_token);
            this.props.history.push("/dashboard");
          });
      }

0 个答案:

没有答案
相关问题