英語わかんね!←おい!
というのは半分冗談で(おい!)、うまくLinqToTwitterで認証画面を出すことができなかったんですよね。
と、いうわけで思いついたのがTwitterizerを使用しての認証だけ突破しよう!
Twitterizerなら認証はやったことがあるし、なによりaccsesTokenとaccessSecreとconsumerKeyとconsumerSecretさえ取得できればいいんじゃね?と思ったから。
というわけで即実践
※コメント等は自分の解釈で書いてたりするのでわかりにくかったりするかもです、もし間違ってたりしたらコメントしてくれるとうれしいです。
また、いらなくねこれっていうのも教えてくれるとありがたいです。
というのは半分冗談で(おい!)、うまくLinqToTwitterで認証画面を出すことができなかったんですよね。
と、いうわけで思いついたのがTwitterizerを使用しての認証だけ突破しよう!
Twitterizerなら認証はやったことがあるし、なによりaccsesTokenとaccessSecreとconsumerKeyとconsumerSecretさえ取得できればいいんじゃね?と思ったから。
というわけで即実践
※コメント等は自分の解釈で書いてたりするのでわかりにくかったりするかもです、もし間違ってたりしたらコメントしてくれるとうれしいです。
また、いらなくねこれっていうのも教えてくれるとありがたいです。
今回もForm1のソースをまるまる直置き
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LinqToTwitter;
using System.Diagnostics;
using System.Configuration;
//新たに増やしたところ
using Twitterizer;
using System.IO;
using System.Xml;
using System.Net;
namespace じみったー
{
public partial class Form1 : Form
{
//PIN_Form PinForm = null;
private string pincode = "";
PinAuthorizer auth;
public string accessToken = "";
public string accessSecret = "";
public string consumerKey = "見せれないよ";
public string consumerSecret = "見せれないよ";
OAuthTokenResponse req;
string pin = "";
OAuthTokenResponse actToken;
string initfile = "TwiZimiPin.xml"; //トークン保存・設定保存ファイルの作成準備
TwitterContext twitterCtx;
public Form1()
{
InitializeComponent();
}
//Pin_Formから値を受け取る
public string Pinform
{
set
{
try
{
pincode = value;
pin = pincode;
actToken = OAuthUtility.GetAccessToken(consumerKey, consumerSecret, req.Token, pin);
auth.OAuthTwitter.OAuthToken = actToken.Token;
auth.OAuthTwitter.OAuthTokenSecret = actToken.TokenSecret;
twitterCtx = new TwitterContext(auth);
string xml = "\n" + "\n" + " " + actToken.Token + " \n" + " " + actToken.TokenSecret + " \n" + " ";
FileStream fs = new FileStream(initfile, FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(xml);
sw.Close();
fs.Close();
MessageBox.Show("認証成功", "じみったー", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "認証失敗", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
get
{
return pincode;
}
}
//認証が押された
private void PinToolStripMenuItem_Click(object sender, EventArgs e)
{
if (accessToken == "") //アクセストークンが指定されてるか
{
auth = new PinAuthorizer();
auth.Credentials = new InMemoryCredentials
{
ConsumerKey = this.consumerKey,
ConsumerSecret = this.consumerSecret
};
auth.UseCompression = false;
req = OAuthUtility.GetRequestToken(consumerKey, consumerSecret, "oob");
Process.Start(OAuthUtility.BuildAuthorizationUri(req.Token).ToString());
PIN_Form PinForm = new PIN_Form(this); //Foem1が参照できるように引数はthis
PinForm.TopMost = this.TopMost; //Form1のTopMostの状態を代入
PinForm.ShowDialog(); //PinFormを表示
}
else
{
MessageBox.Show("すでに認証されてます。", "じみったー", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//Form1が読み込まれた
private void Form1_Load(object sender, EventArgs e)
{
LoadInit();
if (accessToken != "") //アクセストークンが指定されてるか
{
auth = new PinAuthorizer();
auth.Credentials = new InMemoryCredentials
{
ConsumerKey = this.consumerKey,
ConsumerSecret = this.consumerSecret
};
auth.OAuthTwitter.OAuthToken = this.accessToken;
auth.OAuthTwitter.OAuthTokenSecret = this.accessSecret;
twitterCtx = new TwitterContext(auth);
}
else
{
MessageBox.Show("認証情報がありません、認証してください。", "じみったー", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
/// 設定値を読み込み
private bool LoadInit()
{
if (File.Exists(initfile))
{
XmlDocument doc = new XmlDocument();
FileStream fs = new FileStream(initfile, FileMode.Open);
doc.Load(fs);
XmlElement root = doc.DocumentElement;
accessToken = root.GetElementsByTagName("accessToken")[0].InnerText;
accessSecret = root.GetElementsByTagName("accessSecret")[0].InnerText;
fs.Close();
}
return true;
}
//TL取得
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = "";
var tweets =
from tweet in twitterCtx.Status
where tweet.Type == StatusType.Home
select tweet;
foreach (var tweet in tweets)
{
textBox2.Text += (tweet.User.Name + "\r\n" +
tweet.Text + "\r\n" + "\r\n\r\n");
}
}
//つぶやき入力欄に入力されたか
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (textBox1.Text != "")
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
//ツイートボタンが押された
private void button1_Click(object sender, EventArgs e)
{
var tweet = twitterCtx.UpdateStatus(textBox1.Text);
}
}
}
0 件のコメント:
コメントを投稿