HeyCHのブログ

慢性疲労のへいちゃんです

【C#】CoreTweetを使ってみよう その1

今回の記事は前回の記事の続きという事で書いていきます。
heych.hatenablog.com

プロジェクトの準備

新規でWPFアプリケーションを作成し、NuGetパッケージマネジャーから
f:id:HeyCH:20200521013526p:plain
「CoreTweet」を検索してインストールします。
f:id:HeyCH:20200521013644p:plain

<Window x:Class="FollowerFollow.MainWindow"
        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"
        xmlns:local="clr-namespace:FollowerFollow"
        mc:Ignorable="d"
        Title="MainWindow" Height="300" Width="400">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
            <RowDefinition Height="25"/>
        </Grid.RowDefinitions>
        <Button Grid.Row="0" Content="認証" Click="Button_Click"/>
        <TextBox Grid.Row="1" Text="{Binding PINCode,UpdateSourceTrigger=PropertyChanged}"/>
        <Button Grid.Row="2" Content="設定" Click="Button_Click_1"/>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.ComponentModel;
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;

namespace FollowerFollow {
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window {
        ViewModel vm;
        public MainWindow() {
            InitializeComponent();
            vm = new ViewModel();
            this.DataContext = vm;
        }

        private void Button_Click(object sender, RoutedEventArgs e) {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e) {

        }
    }
    public class ViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        string _PINCode = "";
        public string PINCode {
            get { return _PINCode; }
            set {
                _PINCode = value;
                OnPropertyChanged("PINCode");
            }
        }
    }
}

アプリケーションを認証します

以下のコードで認証と「C# からこんばんは」というツイートをします。

using System;
using System.Collections.Generic;
using System.ComponentModel;
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;
using static CoreTweet.OAuth;

namespace FollowerFollow {
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window {
        ViewModel vm;
        public MainWindow() {
            InitializeComponent();
            vm = new ViewModel();
            this.DataContext = vm;
        }
        OAuthSession session;
        private void Button_Click(object sender, RoutedEventArgs e) {
            session = OAuth.Authorize(Properties.Resources.ConsumerKey, Properties.Resources.ConsumerSecret);
            System.Diagnostics.Process.Start(session.AuthorizeUri.AbsoluteUri);
        }
        Tokens tokens;
        private void Button_Click_1(object sender, RoutedEventArgs e) {
            tokens = OAuth.GetTokens(session, vm.PINCode);
            tokens.Statuses.Update(status => "C# からこんばんは");
        }
    }
    public class ViewModel : INotifyPropertyChanged {
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string name) {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
        string _PINCode = "";
        public string PINCode {
            get { return _PINCode; }
            set {
                _PINCode = value;
                OnPropertyChanged("PINCode");
            }
        }
    }
}

※ConsumerKeyとConsumerSecretは前回の記事の最後で取得したやつです

でもこれ毎回確認画面出る仕様なのかな?面倒臭いですね。明日の課題という事にしておきましょうか。(無理そうだけども)