Node.js and Twitter API 1.1

浪尽此生 提交于 2019-12-13 04:36:41

问题


I had a small web app that was using the Twitter API 1.0 user_timeline function to quickly get a user's recent tweets without authentication. However, now every call requires oAuth which I'm terrible at. I know there's an application only authentication method, which is what I want since this is an automated app and not a user based one.

The application was built in node.js so a suggestion for a module that supports app-based oAuth would be great. The main thing is I don't have nor need a callback page, which most assume, since all I'm trying to do is get the last few tweets from a handful of specific Twitter accounts which the app tracks. Likewise any links to good oAuth educational resources or, better yet, Twitter 1.1 HTTP request walkthroughs would be much appreciated.

Thank you for your time.


回答1:


Twitter API 1.1 allows only authenticated requests. But the good news is that the oAuth based authentication is not that difficult. To use it:

  1. Generate the four oAuth keys you need. Go to https://dev.twitter.com/apps/new and register your app.
  2. Install package ntwitter on your machine.
  3. Configure the four keys in your app. See the package page on how to do it.
  4. Construct request and get results. See this page on how to make requests.

I find oAuth to be easier and prefer this way.




回答2:


The package EveryAuth does authentication pretty well, too. Also, ntwitter isn't being updated very regularly right now; I found mtwitter to be much better. I suck at explaining stuff, so I'll just show you how I did it:

            var mtwitter = require('mtwitter');

            var twit = new mtwitter({
                consumer_key: { your app consumer key },
                consumer_secret: { your app consumer secret },
                access_token_key: { get this from oauth or everyauth or whatever },
                access_token_secret: { get this from oauth or everyauth or whatever }
            });

            twit.get('/statuses/user_timeline', { include_entities: true },
                function (err, data) {
                    if (err) {
                        console.log(err.toString());
                    }
                    else console.log(JSON.stringify(data));
            });


来源:https://stackoverflow.com/questions/17101454/node-js-and-twitter-api-1-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!