Skip to content

Instantly share code, notes, and snippets.

@ustreamer-01647
Created October 15, 2014 02:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ustreamer-01647/e01f0e0e1bc710d27f95 to your computer and use it in GitHub Desktop.
Save ustreamer-01647/e01f0e0e1bc710d27f95 to your computer and use it in GitHub Desktop.
testCoreTweet2
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="testCoreTweet2.MainWindow"
Title="MainWindow" Width="525" SizeToContent="Height" d:DesignHeight="345">
<StackPanel>
<StackPanel Orientation="Horizontal">
<Label Content="Account:"/>
<Label x:Name="screennameLabel" Content="unregister" />
<Button x:Name="registButton" Content="regist" Width="75" Click="registButton_Click"/>
</StackPanel>
<TextBox x:Name="viewTextBox" Text="TextBox" Height="225" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" />
<Button x:Name="getTl" Content="getTl" Click="getTl_Click"/>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CoreTweet;
namespace testCoreTweet2
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
internal Tokens tokens;
public MainWindow()
{
InitializeComponent();
// トークン組立
if (!string.IsNullOrEmpty(Properties.Settings.Default.AccessToken)
&& !string.IsNullOrEmpty(Properties.Settings.Default.AccessTokenSecret))
{
tokens = Tokens.Create(
Properties.Settings.Default.ApiKey
, Properties.Settings.Default.ApiSecret
, Properties.Settings.Default.AccessToken
, Properties.Settings.Default.AccessTokenSecret);
updatescreennameLabel();
/*
* http://01647.hateblo.jp/entry/2014/10/12/132505
* CoreTweet.Tokens.Account.VerifyCredentials()とTwitter OAuth2それぞれの調査記録 2014-10-12
*
* 動的にScreenNameを得る場合のテストコード
*/
// トークン有効性確認
//try
//{
// var userResponse = tokens.Account.VerifyCredentials();
// updatescreennameLabel(userResponse.ScreenName);
// Properties.Settings.Default.ScreenName = userResponse.ScreenName;
// Properties.Settings.Default.Save();
//}
//catch (Exception ex)
//{
// // MessageBox.Show(ex.Message);
// tokens = null;
//}
}
}
/// <summary>
/// スクリーンネーム表示を更新する
/// </summary>
/// <param name="screenName">Twitter Screen Name</param>
/// パラメータ省略時は設定ファイルを読み出す
internal void updatescreennameLabel(string screenName = null)
{
string _screenName;
if (string.IsNullOrEmpty(screenName))
{
_screenName = Properties.Settings.Default.ScreenName;
// 未認証時
if (string.IsNullOrEmpty(_screenName))
{
screennameLabel.Content = "unregister";
return;
}
}
else
{
_screenName = screenName;
}
// http://msdn.microsoft.com/ja-jp/library/system.windows.controls.label(v=vs.110).aspx
// WPF Labelにおける文字 _ の仕様について対策する
screennameLabel.Content = _screenName.Replace("_", "__");
}
// Twitter アカウント認証
private void registButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new RegistAccountWindow();
dialog.Owner = this;
// http://msdn.microsoft.com/ja-jp/library/system.windows.window.showdialog(v=vs.110).aspx
dialog.ShowDialog();
}
private void getTl_Click(object sender, RoutedEventArgs e)
{
if (tokens != null)
{
viewTextBox.Clear();
try
{
foreach (var status in tokens.Statuses.HomeTimeline())
{
// http://qiita.com/lambdalice/items/55b1a3d8403ecc603b47#2-4 例1: REST API
// {ユーザー名}: {投稿内容}{改行}
viewTextBox.AppendText(string.Format("{0}: {1}{2}"
, status.User.ScreenName
, status.Text
, Environment.NewLine));
}
}
catch (Exception ex)
{
viewTextBox.AppendText(ex.Message);
}
}
}
}
}
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="testCoreTweet2.RegistAccountWindow"
Title="Get PIN" Width="550" d:DesignHeight="229" SizeToContent="Height" ContentRendered="Window_ContentRendered">
<StackPanel>
<Label Content="Access here:" />
<TextBox x:Name="pinURITextBox" HorizontalScrollBarVisibility="Auto" IsReadOnly="True" Text="processing..."/>
<Label Content="PIN:"/>
<TextBox x:Name="pinTextBox" />
<Button x:Name="okButton" Content="OK" IsDefault="True" Click="okButton_Click"/>
<Button x:Name="cancelButton" Content="Cancel" IsCancel="True" Click="cancelButton_Click"/>
</StackPanel>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using CoreTweet;
namespace testCoreTweet2
{
/// <summary>
/// RegistAccountWindow.xaml の相互作用ロジック
/// </summary>
public partial class RegistAccountWindow : Window
{
OAuth.OAuthSession session;
public RegistAccountWindow()
{
InitializeComponent();
}
private void Window_ContentRendered(object sender, EventArgs e)
{
// http://msdn.microsoft.com/ja-jp/library/system.windows.window.contentrendered(v=vs.110).aspx
initAuthrize();
}
// OAuthセッションを作る
private void initAuthrize()
{
try
{
session = OAuth.Authorize(Properties.Settings.Default.ApiKey, Properties.Settings.Default.ApiSecret);
pinURITextBox.Text = session.AuthorizeUri.ToString();
pinTextBox.Clear();
// System.Diagnostics.Process.Start(session.AuthorizeUri.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Close();
}
}
private void cancelButton_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
private void okButton_Click(object sender, RoutedEventArgs e)
{
// http://msdn.microsoft.com/ja-jp/library/system.text.regularexpressions.regex.aspx
// http://msdn.microsoft.com/ja-jp/library/az24scfc.aspx 正規表現言語 - クイック リファレンス
// PINに数字以外を含む場合,認証に移行しない
if (string.IsNullOrEmpty(pinTextBox.Text)
|| System.Text.RegularExpressions.Regex.IsMatch(pinTextBox.Text, @"\D"))
{
MessageBox.Show("Type numeric characters");
pinTextBox.Clear();
return;
}
try
{
// PIN認証
MainWindow owner = (MainWindow)this.Owner;
owner.tokens = session.GetTokens(pinTextBox.Text);
// トークン保存
Properties.Settings.Default.AccessToken = owner.tokens.AccessToken;
Properties.Settings.Default.AccessTokenSecret = owner.tokens.AccessTokenSecret;
Properties.Settings.Default.ScreenName = owner.tokens.ScreenName;
Properties.Settings.Default.Save();
// 表示調整
owner.updatescreennameLabel(owner.tokens.ScreenName);
MessageBox.Show("verified: " + owner.tokens.ScreenName);
Close();
}
catch (Exception ex)
{
// やり直し
MessageBox.Show(ex.Message);
initAuthrize();
}
}
}
}
@ustreamer-01647
Copy link
Author

http://01647.hateblo.jp/entry/2014/10/15/112245
C# WPF初心者のCoreTweet入門4 別ソリューションを作成し,入門1-3のまとめとWPFの勉強 2014-10-15
https://gist.github.com/ustreamer-01647/e01f0e0e1bc710d27f95/2182f1b8d07a8e93e985ee991ffc565c0254c171

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment