使用 react-native 从节点服务器请求 OAuth2 令牌

Requesting OAuth2 tokens from a node server using react-native

作为学习 OAuth2 和使用 react-native 持久登录的一部分,我使用找到的 OAuth2 服务器示例 here 来了解如何从服务器发送信息和请求验证。

目前,我只是希望能够将 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>
    )
  }
}

包含的自述文件建议如下,但我似乎找不到正确发送 headers/fetch 的示例?

### 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`)

我目前在 React native 中使用 OAuth 2,我发现了一个非常好的博客在谈论它。它还有一个 github 存储库供参考。希望对你有帮助。

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