我正在尝试fetch在 React Native 中使用从 Product Hunt API 中获取信息。我已经获得了正确的访问令牌并将其保存到 State,但似乎无法在 GET 请求的 Authorization 标头中传递它。
这是我到目前为止所拥有的:
var Products = React.createClass({
  getInitialState: function() {
    return {
      clientToken: false,
      loaded: false
    }
  },
  componentWillMount: function () {
    fetch(api.token.link, api.token.object)
      .then((response) => response.json())
      .then((responseData) => {
          console.log(responseData);
        this.setState({
          clientToken: responseData.access_token,
        });
      })
      .then(() => {
        this.getPosts();
      })
      .done();
  },
  getPosts: function() {
    var obj = {
      link: 'https://api.producthunt.com/v1/posts',
      object: {
        method: 'GET',
        headers: {
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'Authorization': 'Bearer ' + this.state.clientToken,
          'Host': 'api.producthunt.com'
        }
      }
    }
    fetch(api.posts.link, obj)
      .then((response) => response.json())
      .then((responseData) => {
        console.log(responseData);
      })
      .done();
  },
我对我的代码的期望如下:
- 首先,我将
fetch使用导入的 API module中的数据获取访问令牌 - 之后,我会将 的
clientToken属性设置this.state为等于收到的访问令牌。 - 然后,我将运行
getPosts它应该返回一个包含来自 Product Hunt 的当前帖子数组的响应。 
我能够验证是否正在接收访问令牌并将this.state其作为其clientToken财产接收。我还能够验证getPosts正在运行。
我收到的错误如下:
{"error":"unauthorized_oauth", "error_description":"请提供有效的访问令牌。有关如何授权 api 请求,请参阅我们的 api 文档。另请确保您需要正确的范围。例如 \"private public\ " 用于访问私有端点。"}
我一直在假设我没有在我的授权标头中正确传递访问令牌,但似乎无法弄清楚确切原因。