Just another WordPress site

すぽんちょぶろぐ

未分類

CoreTweet の Test

投稿日:

CoreTweetの動作テスト
WindowsRT,PINコード使用

MainPage.xaml

<Pagex:Class="CoreTweetTest.MainPage"
<Page
x:Class="CoreTweetTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:CoreTweetTest"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">

<Grid
Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel Margin="80,80,80,80">
<Button Click="OnAuthorize">1st. Authorize Via External Web Browser</Button>
<Button Click="PinExeCute">2nd. PIN Authoritarian</Button>
<TextBox x:Name="_pincodeTextBox" Text="Put PIN" />
<Button Click="OnTweet">Tweet Now</Button>
<TextBox x:Name="_statusTextBox" Text="Message" />
</StackPanel>
<StackPanel Grid.Row="1" Margin="75,75,75,0">
<TextBlock TextWrapping="Wrap" Margin="0,0,0,0" Text="Message" />
<ScrollViewer Height="200" VerticalScrollBarVisibility="Auto" BorderThickness="1" BorderBrush="RoyalBlue" Opacity="0.5" >
<TextBlock x:Name="_debugMessage" TextWrapping="Wrap" Text="" />
</ScrollViewer>
</StackPanel>
</Grid>
</Page>

MainPage.xaml.cs

MainPage.xamlのコードビハインド。

using CoreTweet;
using System;
using System.Threading;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=234238 を参照してください

namespace CoreTweetTest
{
///
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
///

public sealed partial class MainPage : Page
{
#region Fields

internal const string API_CONSUMER_TOKEN = "{ConsumerKey}";
internal const string API_CONSUMER_SECRET = "{ConsumerSecret}";
internal const string API_CONSUMER_CALLBACK_URL = "";

#endregion Fields

#region Properies

public OAuth.OAuthSession Session { get; private set; }

public Tokens Tokens { get; private set; }

#endregion Properies

#region Constructor

public MainPage()
{
this.InitializeComponent();
_debugMessage.Text = "Start..";
}

#endregion Constructor

#region Methods

///
/// 1st.Step OnAuthorize
///

private async void OnAuthorize(object sender, Windows.UI.Xaml.RoutedEventArgs args)
{
Session = await OAuth.AuthorizeAsync(API_CONSUMER_TOKEN, API_CONSUMER_SECRET);
string uriToLaunch = Session.AuthorizeUri.ToString();
var uri = new Uri(uriToLaunch);
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
return;
}
else
{
// URI launch failed
MessageDialog md = new MessageDialog("Failed External Web Browser Open.");
await md.ShowAsync();
}
}

///
/// 2nd.Step PIN Comparison Button
///

private async void PinExeCute(object sender, RoutedEventArgs e)
{
string pin = _pincodeTextBox.Text;
Tokens = await Session.GetTokensAsync(pin);
if (Tokens != null)
{
_debugMessage.Text +=
"r" +
"r Token:" + Tokens.AccessToken.ToString() +
"r TokenSecret :" + Tokens.AccessTokenSecret.ToString() +
"r UseerId :" + Tokens.UserId.ToString() +
"r ScreenName :" + Tokens.ScreenName.ToString();
}
}

///
/// OnTweet Button
///

private async void OnTweet(object sender, Windows.UI.Xaml.RoutedEventArgs args)
{
var tokenSource = new CancellationTokenSource();
var res = await Tokens.Statuses.UpdateAsync(new { status = _statusTextBox.Text + " #Test #CoreTweet #KunappWSA" }, tokenSource.Token);
tokenSource.Cancel();
if (res != null)
{
_debugMessage.Text +=
"r result :" +
"r Id:" + res.Id.ToString() +
"r HashCode:" + res.GetHashCode().ToString() +
"r Json:" + res.Json.ToString() +
"r Text:" + res.Text;
return;
}
}
#endregion Methods
}
}

実行画面

GitHub

-未分類

Copyright© すぽんちょぶろぐ , 2021 All Rights Reserved.