LoginSignup
2
3

More than 1 year has passed since last update.

【TwitterAPI】Github草みたいなTwi草を作ってみた件

Last updated at Posted at 2022-09-10

こんなのを作ってみました。
image.png

Githubの草とは

GithubのOverviewで見れるこいつのことです。(プライベートアカウントなので緑地少なめです)
今回はASP.NET MVC 5(C#)で作ってみました。
image.png

TwitterAPIを使うには

APIキー・トークンです。詳しくは「はじめてのTwitterAPI(やさしめ解説)」を読んでみてね。

TweetinviAPIを使う

楽したかったので「TweetinviAPI」というNuGetパッケージを使っています。

NuGetパッケージの入れ方

ここから開いて
image.png

「TweetinviAPI」で検索すると出てきます。
image.png

実際のコード

こんな感じで各種キーを渡せば認証ができます。
今回は指定したツイッターアカウントのタイムラインを取得する「Tweetinvi.Timeline.GetUserTimeline」メソッドを使っています。
※どうやら取得できる最大が200件のよう

レスポンスで投稿時間とかが取得できるのでいい感じに割り振って画面表示させています。

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Tweetinvi;
using Tweetinvi.Models;

namespace SETools.Controllers
{
	public class WeeklyTwitterContributionsController : Controller
	{
		[HttpPost]
		public ActionResult Result(string twitterAccount)
		{
			//Twitter認証用セッション変数
			string api_key = "hogehoge";
			string api_secret_key = "hogehoge";
			string access_token = "hogehoge";
			string access_token_secret = "hogehoge";

            // 認証
            Auth.SetUserCredentials(api_key, api_secret_key, access_token, access_token_secret);

			// 直近200件のtweet取得
			var tweets = Tweetinvi.Timeline.GetUserTimeline(twitterAccount, 200);

成果物

今回作ったプログラムは以下のページにあります。
是非使ってみてください。
https://setools.tokyo/WeeklyTwitterContributions

2
3
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
2
3