LINQ to Twitter を利用してASP.NETアプリケーションでTwitter認証する (C#プログラミング)
LINQ to Twitter を利用してASP.NETアプリケーションでTwitter認証するコードを紹介します。
プロジェクトへのLINQ to Twitterのインストール
こちらの記事を参照してnuGetを利用して、プロジェクトにLINQ to Twitterをインストールします。
インストールされると、参照設定にアセンブリがたくさん追加されます。
サンプルプログラム
UI
下図のUIを作成します。ボタンとラベルを2つ配置します。
aspxの非同期対応
LINQ to Twitterでは非同期メソッドを利用します。デフォルトのままの設定では非同期メソッドを使用できないため、aspxファイルの先頭のPageディレクティブに下記の記述を追加します。
Async="true"
Aync="true" を記述語のページディレクティブは以下になります。
<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TwitterAuth.Default" %>
なお、非同期メソッド(async await)の詳細は「
await async を用いたシンプルな非同期メソッドの作成と利用 (C#プログラミング)」の記事を参照してください。
aspxファイルのコードは下記になります。
<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TwitterAuth.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button_Auth" runat="server" Text="認証" OnClick="Button_Auth_Click" />
</div>
<div>
OAuthToken:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div>
OAuthTokenSecret:<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
コード
下記のコードを記述します。ButtonのClickイベントとページのLoadイベントにコードを記述します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LinqToTwitter;
namespace TwitterAuth
{
public partial class Default : System.Web.UI.Page
{
private AspNetAuthorizer auth;
protected async void Page_Load(object sender, EventArgs e)
{
auth = new AspNetAuthorizer();
auth.CredentialStore = new SessionStateCredentialStore();
auth.CredentialStore.ConsumerKey = "(アプリケーションのコンシューマーキー)";
auth.CredentialStore.ConsumerSecret = "(アプリケーションのコンシューマーシークレット)";
auth.GoToTwitterAuthorization = twitterUrl => Response.Redirect(twitterUrl, false);
if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null) {
await auth.CompleteAuthorizeAsync(Request.Url);
Label1.Text = auth.CredentialStore.OAuthToken;
Label2.Text = auth.CredentialStore.OAuthTokenSecret;
}
}
protected async void Button_Auth_Click(object sender, EventArgs e)
{
await auth.BeginAuthorizeAsync(Request.Url);
}
}
}
解説
ページ表示時に下記のコードにより、AspNetAuthorizerを作成します。
アプリケーションのコンシューマーキーの取得は
こちらの記事を参照してください。
auth = new AspNetAuthorizer();
auth.CredentialStore = new SessionStateCredentialStore();
auth.CredentialStore.ConsumerKey = "(アプリケーションのコンシューマーキー)";
auth.CredentialStore.ConsumerSecret = "(アプリケーションのコンシューマーシークレット)";
auth.GoToTwitterAuthorization = twitterUrl => Response.Redirect(twitterUrl, false);
認証ボタンがクリックされた場合は、AspNetAuthorizerのBeginAuthorizeAsync()メソッドを呼び出しTwitterの認証処理を開始します。メソッドの第一引数にはコールバックするURLを与えます。
await auth.BeginAuthorizeAsync(Request.Url);
コールバックされた場合は、"oauth_token"パラメーターが付加されているため、"oauth_token"パラメータが存在する場合はコールバックされたと判定し、ラベルにトークンの情報を表示します。
if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null) {
await auth.CompleteAuthorizeAsync(Request.Url);
Label1.Text = auth.CredentialStore.OAuthToken;
Label2.Text = auth.CredentialStore.OAuthTokenSecret;
}
実行結果
プロジェクトを実行します。Webブラウザが起動し、下図の画面が表示されます。上部の[認証]ボタンをクリックします。
ページ遷移しTwitterの認証画面が表示されます。ID,パスワードのテキストボックスに認証するTwitterアカウントの情報を入力します。入力後、[連携アプリを認証]ボタンをクリックします。
元のページに遷移します。ラベルにOAuthのトークン情報が表示されます。
LINQ to Twitterを使用してASP.NETアプリケーションで認証ができました。
登録日 :2015-03-11 最終更新日 :2018-01-05