如何在服务器中实现OAuth2授权?

时间:2017-05-04 05:49:24

标签: ios reactjs react-native oauth-2.0 react-redux

我想在react native中实现OAuth2授权。我通过以下方式实现了代码。

      const HOST_ADRESS = "192.168.173.1"; //change with your own host
        const client_id   = "app";
        const username = "balan@gmail.com";
        const password = "12345";
        fetch('https://'+HOST_ADRESS+"/oauth/token", {
          method: 'POST',
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'Cache-Control': 'no-cache'
          },
          body: "client_id=app&client_secret=app1234&grant_type=pass&username="+username+"&password="+password
        })
        .then((response) => response.json())
            .then((responseJson) => {
              console.log("******* RESPONSE *********  "+ JSON.stringify(responseJson));
            })
        .then((response) => response.text())
        .then((responseText) => {
          console.log("--------------responseText"+responseText);
         console.log("******* RESPONSE *********  "+ JSON.stringify(responseJson));
  })
  .catch((error) => {
    const data = {error: "A error happened"};
    //redux error.
    //dispatch(actionsCreators.errorLogin(data));
      console.log("+++++++++++++++++++error"+error);
    console.warn(error);
  });

但它总是会出现错误,如

{"错误":"未经授权"," error_description":"访问此资源需要完全身份验证"}

0 个答案:

没有答案