この記事で紹介している方法は 2015年時点の方法です。Twitterの仕様変更等でこのコードでは動作しない可能性が高いため、ご注意ください。
<%@ Page Async="true" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TwitterAuth.Default" %>
<%@ 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>
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);
/*
//または下記のコード
auth = new AspNetAuthorizer
{
CredentialStore = new SessionStateCredentialStore
{
ConsumerKey = "(アプリケーションのコンシューマーキー)",
ConsumerSecret = "(アプリケーションのコンシューマーシークレット)"
},
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);
}
}
}
auth = new AspNetAuthorizer();
auth.CredentialStore = new SessionStateCredentialStore();
auth.CredentialStore.ConsumerKey = "(アプリケーションのコンシューマーキー)";
auth.CredentialStore.ConsumerSecret = "(アプリケーションのコンシューマーシークレット)";
auth.GoToTwitterAuthorization = twitterUrl => Response.Redirect(twitterUrl, false);
await auth.BeginAuthorizeAsync(Request.Url);
if (!Page.IsPostBack && Request.QueryString["oauth_token"] != null) {
await auth.CompleteAuthorizeAsync(Request.Url);
Label1.Text = auth.CredentialStore.OAuthToken;
Label2.Text = auth.CredentialStore.OAuthTokenSecret;
}