试图用类型脚本找出一些基本的推特功能。我不知道为什么会这样运行,但不会转发包含#100DaysOfCode 的推文

Trying to figure out some basic twitter functions with type script. I have no idea why this runs, but doesn't retweet tweets containing #100DaysOfCode

感谢帮助。我真的不知道发生了什么。我之前也从未使用过 typescript 或 JavaScript。该代码循环运行,但它实际上并没有转发任何包含主题标签的推文

import Twit, { Response } from 'twit';

const Twitter = new Twit(require('./config'));

const mediaArtsSearch: Twit.Params = {
  q: '#100DaysOfCode',
  count: 100,
  // eslint-disable-next-line @typescript-eslint/camelcase
  result_type: 'recent',
};

const retweetLatest = () => {
  Twitter.get('search/tweets', mediaArtsSearch, (error: Error, data: any) => {
    console.log(error, data);
    if (!error) {
      const retweetId = data.statuses[0].id_str;     //finds id of tweet, labels it as retweetid
      Twitter.post(
        'statuses/retweet/' + retweetId,            //retweets the tweet
        {},
        (error: Error, response: Response) => {
          if (response) {
            console.log(
              'Success! Check your bot, it should have retweeted something.',
            );
          }
          if (error) {
            console.log('There was an error with Twitter:', error);
          }
        },
      );
    } else {
      console.log('There was an error with your hashtag search:', error);
    }
  });
};

retweetLatest();
setInterval(retweetLatest, 1000 * 20);

我认为您在评论中添加的错误消息非常明确:存在授权问题。 您调用 twitter API 的方式确实允许您 GET(搜索推文...)但不允许 POST 推文。

可能有以下几种原因:

  • 您没有(正确地?)向您的请求(令牌等)添加身份验证元素。
  • 您帐户的电子邮件正在等待验证。
  • 您必须在您的 Twitter 开发者帐户设置中允许“读+写”。即使已经选择了读+写,您也可能需要再次单击它(如下所述):

In case this helps anyone else: The twitter dev page already showed read+write access for my app, but the authorization request still only requested read access. I had to click on "read+write" in the dev settings again (despite it already being selected), then click on save. This seems to be a bug on the twitter side of things, probably triggered by the recent API changes.

(在此处找到:https://github.com/DocNow/diffengine/issues/20#issuecomment-807864651