LoginSignup
0
1

More than 1 year has passed since last update.

twitter api v1からv2へ(その2)

Last updated at Posted at 2022-11-21

はじめに

 twitter api v1からv2へを書いたが、oauth pkceについてはまだだったのでそのご報告

手順

 1)twitter developerでの登録

  App permissions=Read and write and Direct message
   Type of App=Web App, Automated App or Bot
   App info
     Callback URI / Redirect URL=http://127.0.0.1:3000/callback
     Website URL=https://twitter.com/
   以下は入力していない

そして、そのあと、Keys and tokensのタブにいって、最後のOAuth 2.0 Client ID and Client Secretの部分で、Client IDとClient Secretをメモする・・といってもcopyしてenvに設定する。
2)twitter-sdkのinstall
上のlinkはgitそのままだが、 npm i twitter-api-sdkでダウンロードできる。
  そのあと、npm i express,@types/express,dotenv

3)copyと環境設定
  https://github.com/twitterdev/twitter-api-typescript-sdk のexampleから
  oauth2-callback_pkce_plain.tsをもってくる。とりあえず、login.tsとした。
  環境変数にCLIENT_IDとCLIENT_SECRETを設定する、設定する値は1)で取得した値

4)実行
  npx ts-node login.ts
で、Go here to login: http://127.0.0.1:3000/login
  というメッセージがでたら、 自分のchromeとかで上のアドレスを叩く
  すると、twitterの画面が出てきて承認しろといわれるので、okする。

 5)確認
  認証後のtweetメッセージが自分のchrome上に表示されるので、これでok

twitter-api-sdkのドキュメント

 installしたnode_module/twitter-api-sdk/dist/gen/client.d.tsあたりに、関数の呼び方の一覧があるので、これを読みながら、関数を探す。timelineを探すと
 client.tweets.usersIdTimeline('userid')という形で呼び出せば、/2/users/:id/timelines/reverse_chronologicalを呼び出すということで、これも成功

 ちなみに、typescriptではなくjavascriptでも大して変わらない。importをrequireに変更してas Stringとかの設定を削除すれば、無事node login.jsということで動く。私はこれで実験した。

あとはよしなに

0
1
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
0
1