【问题标题】:Requesting OAuth2 tokens from a node server using react-native使用 react-native 从节点服务器请求 OAuth2 令牌
【发布时间】:2018-05-05 00:06:31
【问题描述】:

作为学习 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>
    )
  }
}

包含的自述文件建议以下内容,但我似乎找不到正确发送标头/获取的示例?

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

【问题讨论】:

    标签: node.js react-native oauth-2.0 fetch-api


    【解决方案1】:

    我目前正在使用 React Native 中的 OAuth 2,我发现一个非常好的博客谈论它。它也有一个 github repo 供参考。希望对你有帮助。

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

    【讨论】:

    • 谢谢你,看起来是一个不错的项目。
    • 您介意我问一下您在启动和运行它时是否有任何问题?有几个可以更换的折旧包,我已经成功管理,但唯一对我不起作用的是用户注册。我收到与失败的承诺有关的错误。
    • 其实我只是作为参考。我刚刚阅读了博客以获取更多信息。但是你能告诉我一个错误吗?我会尽力帮助
    • 谢谢,我决定从头开始构建类似的东西,从简单的 fetch(RN) 和 response (Node) 开始。一旦第一个功能起作用,我将开始添加每个附加功能。
    • 是的 :D 这比为你的克隆某人来源要好得多 :D 很高兴你让它工作
    猜你喜欢
    • 1970-01-01
    • 2021-08-10
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2018-01-17
    相关资源
    最近更新 更多