地味に変わってる。非同期で動くので私の頭が付いていけない状態。同期してていいんじゃ・・・。
はてなメッセージは取得できた。残念ながらはてブの情報はなかった(´・ω・`)
バックアップ代わりのソース
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; namespace WindowsFormsApp1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); mtp = new Main_http(); } Main_http mtp; public string tmp { get; set; } private void button1_Click(object sender, EventArgs e) { //認証コード取得画面出るまで mtp.Set_authorizeUrl(); } private async void button2_Click(object sender, EventArgs e) { //ピンコード取得 var pinCode = this.textBox1.Text; //アクセストークン取得 await mtp.Set_accsessToken(pinCode); //うまくいったらボタンとか無効にする this.button1.Enabled = false; this.textBox1.Enabled = false; this.button2.Enabled = false; //アイコンとブクマURL表示 this.pictureBox1.ImageLocation = Properties.Settings.Default.profile_image_url; this.textBox2.Text = "http://b.hatena.ne.jp/" + Properties.Settings.Default.url_name; } private void Form1_Load(object sender, EventArgs e) { var p = Properties.Settings.Default.oauth_token; if( p!= "") { //うまくいったらボタンとか無効にする this.button1.Enabled = false; this.textBox1.Enabled = false; this.button2.Enabled = false; //アイコンとブクマURL表示 this.pictureBox1.ImageLocation = Properties.Settings.Default.profile_image_url; this.textBox2.Text = "http://b.hatena.ne.jp/" + Properties.Settings.Default.url_name; } } private void button3_Click(object sender, EventArgs e) { //パラメータ初期化 mtp.Delete_para(); //ボタン有効化 this.button1.Enabled = true; this.textBox1.Enabled = true; this.button2.Enabled = true; this.pictureBox1.ImageLocation = ""; this.textBox2.Text = ""; } private void button4_Click(object sender, EventArgs e) { //Form2クラスのインスタンスを作成する Form2 f = new Form2(); //Form2を表示する //ここではモーダルダイアログボックスとして表示する //オーナーウィンドウにthisを指定する this.Visible = false; f.ShowDialog(this); //終わったらフォーム1表示 this.Visible = true; //フォームが必要なくなったところで、Disposeを呼び出す f.Dispose(); } private async void textBox4_Click(object sender, EventArgs e) { //テキストボックスをクリックしたらはてなメッセージを読み込む var ret= await mtp.test(); this.textBox4.Text = ret; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.IO; using System.Net.Http; using AsyncOAuth; namespace WindowsFormsApp1 { // a sample of hatena client public class HatenaClient { readonly string consumerKey; readonly string consumerSecret; readonly AccessToken accessToken; public HatenaClient(string consumerKey, string consumerSecret, AccessToken accessToken) { this.consumerKey = consumerKey; this.consumerSecret = consumerSecret; this.accessToken = accessToken; } /* // sample flow for Hatena authroize public async static Task<AccessToken> AuthorizeSample(string consumerKey, string consumerSecret) { // create authorizer var authorizer = new OAuthAuthorizer(consumerKey, consumerSecret); // get request token var tokenResponse = await authorizer.GetRequestToken( "https://www.hatena.com/oauth/initiate", new[] { new KeyValuePair<string, string>("oauth_callback", "oob") }, new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>("scope", "read_public,write_public") })); var requestToken = tokenResponse.Token; var pinRequestUrl = authorizer.BuildAuthorizeUrl("https://www.hatena.ne.jp/oauth/authorize", requestToken); // open browser and get PIN Code Process.Start(pinRequestUrl); // enter pin Console.WriteLine("ENTER PIN"); var pinCode = Console.ReadLine(); // get access token var accessTokenResponse = await authorizer.GetAccessToken("https://www.hatena.com/oauth/token", requestToken, pinCode); // save access token. var accessToken = accessTokenResponse.Token; Console.WriteLine("Key:" + accessToken.Key); Console.WriteLine("Secret:" + accessToken.Secret); return accessToken; } */ HttpClient CreateOAuthClient() { return OAuthUtility.CreateOAuthClient(consumerKey, consumerSecret, accessToken); } public async Task<string> GetMy() { var client = CreateOAuthClient(); var json = await client.GetStringAsync("http://n.hatena.com/applications/my.json"); return json; } public async Task<string> GetHatenaMessage() { var client = CreateOAuthClient(); var json = await client.GetStringAsync("http://m2.hatena.com/inbox.json"); return json; } public async Task<string> AppicationStart() { var client = CreateOAuthClient(); var response = await client.PostAsync("http://n.hatena.com/applications/start", new StringContent("", Encoding.UTF8)); return await response.Content.ReadAsStringAsync(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Diagnostics; using System.IO; using System.Net.Http; using AsyncOAuth; using Codeplex.Data; using System.Windows.Forms; //登録 //OAuth認証URL //Temporary Credential Request URL https://www.hatena.com/oauth/initiate //Resource Owner Authorization URL(PC) https://www.hatena.ne.jp/oauth/authorize //Token Request URL https://www.hatena.com/oauth/token namespace WindowsFormsApp1 { class Main_http { private const string OAuthConsumerKey = "*************"; private const string OAuthConsumerSecret = "*************"; //リクエストトークン private RequestToken requestToken { get; set; } //認証URL public string authorizeUrl { get; set; } //アクセストークン public AccessToken accessToken { get; set; } //はてなクライアント public HatenaClient htn; public Task<string> test() { if (htn == null) { accessToken= new AccessToken (Properties.Settings.Default.oauth_token,Properties.Settings.Default.oauth_token_secret); htn = new HatenaClient(OAuthConsumerKey, OAuthConsumerSecret, accessToken); } var ret = htn.GetHatenaMessage(); return ret; } //認証用のURLを設定 public async void Set_authorizeUrl() { var authorizer = new OAuthAuthorizer(OAuthConsumerKey, OAuthConsumerSecret); var callbackUri = "oob"; var requestTokenResponse = await authorizer.GetRequestToken( "https://www.hatena.com/oauth/initiate", new[] { new KeyValuePair<string, string>( "oauth_callback", callbackUri ) }, new FormUrlEncodedContent(new[] { new KeyValuePair<string, string>( "scope", "read_public,write_public,read_private" ) }) ); //リクエストトークン requestToken = requestTokenResponse.Token; //認証用のURL作成 authorizeUrl = authorizer.BuildAuthorizeUrl( "https://www.hatena.ne.jp/touch/oauth/authorize", requestToken); //"https://dobon.net"を標準のブラウザで開いて表示する System.Diagnostics.Process.Start(authorizeUrl); } public async Task Set_accsessToken(string pinCode) { var authorizer = new OAuthAuthorizer(OAuthConsumerKey, OAuthConsumerSecret); try { // get access token var accessTokenResponse = await authorizer.GetAccessToken("https://www.hatena.com/oauth/token", requestToken, pinCode); // save access token. accessToken = accessTokenResponse.Token; Properties.Settings.Default.oauth_token = accessToken.Key; Properties.Settings.Default.oauth_token_secret = accessToken.Secret; htn = new HatenaClient(OAuthConsumerKey, OAuthConsumerSecret, accessToken); var testjaon = await htn.GetMy(); var json = DynamicJson.Parse(testjaon.ToString()); Properties.Settings.Default.url_name = json.url_name; Properties.Settings.Default.display_name = json.display_name; Properties.Settings.Default.profile_image_url = json.profile_image_url; Properties.Settings.Default.Save(); MessageBox.Show("正常に取得しました"); } catch { MessageBox.Show("コードが間違っているようです。再度コピーしてみてください"); } } public void Delete_para() { Properties.Settings.Default.oauth_token = null; Properties.Settings.Default.oauth_token_secret = null; Properties.Settings.Default.url_name = null; Properties.Settings.Default.display_name = null; Properties.Settings.Default.profile_image_url = null; Properties.Settings.Default.Save(); } } }