LoginSignup
17
17

More than 5 years have passed since last update.

node.jsのtwitterモジュールを使ってみた作業メモ (1)

Last updated at Posted at 2016-04-20

注意: ここは作業メモ、大した情報は無いですょ。

なんとなく、node.jsの勉強会に参加してみたりしたので、何かつくろうかなぁと思い、
ひとまずTwitterをいじってみることにしました。
もっと丁寧な解説ページはいくらでもあると思うので、ここは完全に単なる自分の作業メモとして書いていきます。

ひとまず動作させてみる

Twitter developer credentialを取得する

まずは Twitterのサイト からクレデンシャルを取得する必要があるようで。
Create an applicationをつついて、出てきたフォームに必要事項を記入。

  • Name
  • Description
  • Website
  • Callback URL

Callback URL以外は必須みたいですが、Websiteについては、
まだ無ければ placeholder 書いとけってことなので、Qiita のURLでも書いてときます。
アプリ名は、てけとうにtwitter-testとでもしておきます。 ← 後程失敗の原因になります

で、同意して Createもげもげ ってボタンを押したら怒られました。

Error
You must add your mobile phone to your Twitter profile before creating an application. Please read https://support.twitter.com/articles/110250-adding-your-mobile-number-to-your-account-via-web for more information.

ええええええ……。

まぁ、登録しないと何もできなさそうなので、
Twitterのサイトからプロフィールのモバイル登録して、先程のフォームでポチっとなっと…………。

Error
The client application failed validation: Twitter-test is already taken for Name.

うぎゃあああぁぁぁぁ…………。

しょーがないので、 "Rare's Twitter-test" としました……。
これで、無事に色々と生成できたようです。
あ、アプリ名は後で変更できるみたいですね……。

Access Tokenの作成

クレデンシャルだけでなく、トークンも必要みたいです。
Application ManagementのサイトのKey and Access Tokensというタブをつつくと、
下の方に Create my access token というボタンがあるのでつつきます。

node.jsのTwitterモジュール

npmでさくっとインストールします。
他にも色々モジュールあるとかいう話を読んだ気がしますが、漢らしくtwitterと名乗ってるヤツを使ってみます。

$ npm install twitter

あ、なおバージョン的には、debから入れた以下な感じ。

$ npm --version
3.8.3
$ nodejs --version
v5.10.1

テスト

ひとまず、 twitterモジュールのページ にあるサンプルコードを打ちこんでみます。
以下のようにコードを打ちました。

twitter-test.js
var twitter = require('twitter');

var client = new twitter({
    consumer_key:        '先程取得したconsumer key',
    consumer_secret:     '先程取得したconsumer secret',
    access_token_key:    '先程取得したtoken key',
    access_token_secret: '先程取得したtoken secret',
});

var params = {screen_name: 'nodejs'};

client.get('statuses/user_timeline', params, function(error, tweets, response){
    if (!error) {
        console.log(tweets);
    }
});

で、以下のように実行。

$ nodejs twitter-test.js

とすると、なんかベロベロと表示されました。
twitterのサイト経由でnodejsのツイートを見てみると、同じような内容があるので、ちゃんと取得できたもよう。

[ { created_at: 'Wed Apr 20 13:41:47 +0000 2016',
    id: 722782317186392000,
    id_str: '722782317186392064',
    text: '@AfroJme @js_digest @sumithanathan strange... It works on desktop & if you open it in your email. Working o
n how to fix this now.',
    entities: { hashtags: [], symbols: [], user_mentions: [Object], urls: [] },
    truncated: false,
    source: '<a href="http://twitter.com" rel="nofollow">Twitter Web Client</a>',
    in_reply_to_status_id: 722780639360106500,
    in_reply_to_status_id_str: '722780639360106496',
    in_reply_to_user_id: 44491207,
    in_reply_to_user_id_str: '44491207',
    in_reply_to_screen_name: 'AfroJme',
    user: 
     { id: 91985735,
       id_str: '91985735',
       name: 'Node.js',
       screen_name: 'nodejs',
       location: 'Earth',
       description: 'The Node.js JavaScript Runtime',
       url: 'https://t.co/X32n3a0B1h',
       entities: [Object],
       protected: false,
       followers_count: 337876,
       friends_count: 257,
       listed_count: 5214,
       created_at: 'Mon Nov 23 10:57:50 +0000 2009',
       favourites_count: 325,
       utc_offset: -25200,
       time_zone: 'Pacific Time (US & Canada)',
       geo_enabled: false,
       verified: false,
       statuses_count: 2447,
       lang: 'en',
       contributors_enabled: false,
...

コード中で使ってる "statuses/user_timeline" ってのについては、
REST APIのマニュアルGET statuses/user_timeline のところを読めばオプションやら色々と書いてあるようで。

色々と試してみよう……。

参考

17
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
17