【问题标题】:Twitter API and ReactTwitter API 和 React
【发布时间】:2018-05-07 00:08:12
【问题描述】:

我现在只是在搞乱 React,并认为我会尝试制作一个可以搜索推文的应用程序。我基本上只是遵循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>
    );
  }
}

这只是一个快速测试,您单击按钮并搜索推文并将其记录到控制台。问题是我每次都得到“未定义”,并且我检查了密钥和令牌,所以我认为这不是问题。 我从未尝试过使用此 API,因此非常感谢任何帮助

【问题讨论】:

  • 可能有错误,你可以这样继续if (!error) { console.log(tweets);} else {console.log(error)}。在这种情况下,如果有错误,它们将被记录到控制台
  • 你确定你的推特配置正确,可以正常连接推特

标签: javascript reactjs twitter


【解决方案1】:

twitter api 似乎不支持 CORS 请求,因此我们无法通过浏览器对 api 进行任何调用,因此它不适合您。 请参阅以下链接了解更多详情: https://twittercommunity.com/t/will-twitter-api-support-cors-headers-soon/28276/4

【讨论】:

    猜你喜欢
    • 2016-05-03
    • 2012-10-08
    • 1970-01-01
    • 2021-05-05
    • 2012-08-19
    • 2012-07-05
    • 2011-10-20
    • 2011-05-05
    • 2013-06-10
    相关资源
    最近更新 更多