Twitter API和React

问题描述 投票:1回答:1

我现在只是反应混乱,以为我会尝试制作一个可以搜索推文的应用。我基本上只是关注npm twitter示例,但无法使其正常工作。这是我所拥有的:

import React, { Component } from 'react';
import Twitter from 'twitter';
import config from './config.js'



class App extends Component {
  constructor(){
    super();
    this.Client = new Twitter(config);
    this.searchTwitter = this.searchTwitter.bind(this);
  }

  searchTwitter(){
    this.Client.get('search/tweets', {q: 'node.js'}, function(error, tweets, response) {
   console.log(tweets);
 });
   }

  render() {
    return (
      <div className="App">
        <header className="App-header">
          <img src={logo} className="App-logo" alt="logo" />
          <h1 className="App-title">The Test App</h1>
        </header>
        <button style = {{height: "50px", width: "150px"}} onClick = {this.searchTwitter}> CLICK ME </button>
      </div>
    );
  }
}

这只是一项快速测试,您可以单击按钮,然后搜索推文并将它们记录到控制台。问题是我每次都会得到“ undefined”,并且我已经检查了密钥和令牌,所以我不认为这是问题所在。我从未尝试过使用此API,因此非常感谢您提供帮助

javascript reactjs twitter
1个回答
0
投票

似乎twitter api不支持CORS请求,因此我们无法通过浏览器对该api进行任何调用,因此它对您不起作用。请参考以下链接以获取更多详细信息:https://twittercommunity.com/t/will-twitter-api-support-cors-headers-soon/28276/4

© www.soinside.com 2019 - 2024. All rights reserved.