Node.jsからTwitterにつぶやく

※インストール手順はCentOS7環境で行います。

1. 最初にNode.jsをインストール

最初にNode.jsをインストールします。
下記の2つのコマンドでできます。

curl --silent --location https://rpm.nodesource.com/setup_8.x | sudo bash -
sudo yum -y install nodejs gcc-c++ make

2. 必要なパッケージをインストール

下記のコマンドを実行してください。

npm install twitter
npm install fs

3. Twitterのアクセストークンを取得

取得方法はここでは説明しません。
こちらのサイトより登録してください。
https://apps.twitter.com/

登録方法の解説はこちらを御覧ください
https://qiita.com/kngsym2018/items/2524d21455aac111cdee

4. Twitterのトークンファイルを作る

まず secret.json というファイルを作ってその中にこのコードを書いてください。

{
"consumer_key" :"先ほど取得したConsumer Key(API key)の値",
"consumer_secret" :"先ほど取得したConsumer Secret(API Secret)の値",
"access_token_key" :"先ほど取得したAccess Tokenの値",
"access_token_secret" :"先ほど取得したAccess Token Secretの値"
}

かけたらsecret.jsonを保存して終了します。

5. さっそくコードを書いてみる

次に本題のコードを書いていきたいと思います。まず App.js というファイルを作ってその中にコードを書きます。基本コピペでいいと思います。

const twitter = require("twitter");
const fs = require("fs");
const twitter1 = new twitter(JSON.parse(fs.readFileSync("./secret.json","utf-8")));
twitter1.post('statuses/update', {status: ここにツイートする内容を書く}, function(tweet){
console.log(tweet);
});

このコードだけでできます。

6. 最後に

このコードを使えばもっともっといろいろなことができると思うのでみなさんもこのコードを使って頑張ってみてください!

Node.jsからTwitterにつぶやく” に対して1件のコメントがあります。

  1. Jimmy より:

    I am sure this article has touched all the internet
    people, its really really good post on building up new weblog.
    It’s appropriate time to make some plans for the future and it is time to be happy.
    I have read this post and if I could I want to
    suggest you some interesting things or tips. Perhaps you could write next
    articles referring to this article. I desire to read more things
    about it! I’ll right away clutch your rss feed as
    I can not to find your e-mail subscription hyperlink or e-newsletter service.
    Do you have any? Kindly let me recognise so that I could subscribe.
    Thanks. http://cspan.org

    1. Yopon より:

      Thank you for your help.
      Please be patient as we will add notification and other features via email in the future.

  2. Jimmy より:

    I am sure this paragraph has touched all the internet people,
    its really really good piece of writing on building up new web site.
    It’s perfect time to make a few plans for the longer term and
    it’s time to be happy. I have learn this put up and if I may I want to recommend you some interesting things or tips.
    Maybe you could write next articles relating to this
    article. I desire to learn even more things approximately it!
    I’ve been browsing online more than three hours today, yet I never found any interesting article like yours.
    It is pretty worth enough for me. Personally, if all site owners and bloggers made good content as you did, the
    net will be a lot more useful than ever before. http://www.cspan.net/

    1. Yopon より:

      Thank you for your help.

  3. Kiersten より:

    Hello! Someone in my Facebook group shared this site with us so I came to take a look.

    I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers!
    Great blog and brilliant style and design. Thank you for the auspicious
    writeup. It in fact was a amusement account it. Look advanced to more added agreeable from you!

    By the way, how can we communicate? I will immediately grasp your rss feed as I can’t in finding your email subscription link or newsletter service.
    Do you have any? Please permit me realize in order that
    I may just subscribe. Thanks. http://samsung.com

    1. Yopon より:

      Thank you for your help.
      It corresponds by mail.
      The email address is admin@yopon.jp.

  4. 사설토토 より:

    My partner and I absolutely love your blog and find most of
    your post’s to be what precisely I’m looking for. Does one offer guest
    writers to write content in your case? I wouldn’t mind creating a post
    or elaborating on many of the subjects you write concerning here.
    Again, awesome web log!

    1. Yopon より:

      Thank you for your help.

  5. Hi to every body, it’s my first go to see
    of this webpage; this blog consists of remarkable and in fact excellent data in favor of readers.

    1. Yopon より:

      Thank you for your help.

  6. 먹튀 より:

    I was curious if you ever considered changing the layout of
    your website? Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

    1. Yopon より:

      I will do my best.

  7. 사설토토 より:

    Great web site you have here.. It’s hard to find
    good quality writing like yours these days.
    I really appreciate individuals like you!
    Take care!!

    1. Yopon より:

      Thank you for your help.

  8. Hey There. I found your blog using msn. This is a really well written article.
    I will be sure to bookmark it and come back to read more of your useful info.
    Thanks for the post. I’ll definitely return. https://www.mytotosure.com

    1. Yopon より:

      Thank you for your help.

  9. Sourgeok より:

    Buy generic cialis without prescription buy cialis online vicodin and cialis generic buy cheap cialis online buying generic cialis buy generic cialis online cialis 10mg vs 20mg buy cheap cialis online viagra coupons buy generic viagra online

    1. Yopon より:

      I’m sorry. I do not understand that well.

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA