使用React-Native从节点服务器请求OAuth2令牌



作为学习OAuth2的一部分,并使用React-Native持续登录,我正在使用此处找到的OAuth2服务器示例来弄清楚如何从服务器发送信息和请求验证。<<<<<<<<<<<<

目前,我希望能够将access_token记录到控制台上,因为我认为我可以自己建立这些知识。我似乎找不到一个理想的例子,说明它如何正确获取。

我认为我没有得到的部分是 headerbody信息的发送。

export default class App extends Component {
  goGetToken() {
    return fetch('http://localhost:3000/oauth/token/?grant_type=password&username=pedroetb&password=password', {
      method: 'POST',
      headers: {
        'Authorization': 'Basic YXBwbGljYXRpb246c2VjcmV0',
        'Content-Type': 'application/application/x-www-form-urlencoded',
      },
    }).then(response => response.json())
      .then(responseJson => {
        console.log(responseJson.access_token);
        return responseJson.access_token;
      })
      .catch(error => {
        console.error(error);
      });
  }
  componentDidMount() {
    this.goGetToken();
  }
  render() {
    return(
      <View></View>
    )
  }
}

随附的读数建议以下建议,但是我似乎找不到正确发送标头/获取的示例?

### Obtaining a token
To obtain a token you should POST to `http://localhost:3000/oauth/token`.
#### With *password* grant
You need to include the client credentials in request headers and the user credentials and grant type in request body:
* **Headers**
    * **Authorization**: `"Basic " + clientId:secret base64'd`
        * (for example, to use `application:secret`, you should send `Basic YXBwbGljYXRpb246c2VjcmV0`)
    * **Content-Type**: `application/x-www-form-urlencoded`
* **Body**
    * `grant_type=password&username=pedroetb&password=password`
        * (contains 3 parameters: `grant_type`, `username` and `password`)

我目前与oauth 2在react Native中合作,我发现了一个非常好的博客谈论它。它也有一个github存储库供参考。希望它能为您提供帮助。

https://medium.com/@alexmngn/the-sential--boilerplate-to-authenticate-users-users-on-your-react-native-native-app-f7a8e0e04a4a42

最新更新