この記事で紹介している方法は 2015年時点の方法です。Twitterの仕様変更等でこのコードでは動作しない可能性が高いため、ご注意ください。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using LinqToTwitter;
namespace TwitterAuthWindowsForm
{
public partial class FormMain : Form
{
private PinAuthorizer pa;
public FormMain()
{
InitializeComponent();
pa = new PinAuthorizer();
}
async private void button1_Click(object sender, EventArgs e)
{
pa.CredentialStore = new InMemoryCredentialStore();
pa.CredentialStore.ConsumerKey = "(コンシューマーキー)";
pa.CredentialStore.ConsumerSecret = "(コンシューマーシークレット)";
pa.GoToTwitterAuthorization = pageLink => Process.Start(pageLink);
await pa.BeginAuthorizeAsync();
}
async private void button2_Click(object sender, EventArgs e)
{
await pa.CompleteAuthorizeAsync(textBox1.Text);
textBox2.Text += string.Format("OAuthToken:{0}\r\n", pa.CredentialStore.OAuthToken);
textBox2.Text += string.Format("OAuthTokenSecret:{0}\r\n", pa.CredentialStore.OAuthTokenSecret);
textBox2.Text += string.Format("ScreenName:{0}\r\n", pa.CredentialStore.ScreenName);
textBox2.Text += string.Format("UserID:{0:d}\r\n", pa.CredentialStore.UserID);
}
}
}