OneDriveをC#で使う方法

17 March 2015
#CSharp#OneDrive#LiveSDK

分からなすぎて辛い思いをしたのでメモしておきます。

アプリケーションの登録

先ず、クライアントIDを取得するために、アカウントデベロッパーセンターでアプリケーションを登録します。

Windowsストアアプリの場合は別のところに登録するようですが、iOSやAndroid、デスクトップアプリケーションなどは上記ページからID等を発行すれば良いと思います。

LiveSDKのダウンロード

このSDKをどうやってダウンロードすれば良いのかで数時間飛ばされました。

普通に、nugetにあったっていうね!(nugetも探したはずなのにorz

PM> Install-Package LiveSDK
だそうです(nuget)。

ちなみにインストーラーも配布されてました(ここ)。

 サンプルプログラム

デスクトップアプリケーション(WPF)からOneDriveに接続するためのソースコードです。
<Window x:Class="OneDriveTester.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:OneDriveTester"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        
        <Button Content="Sign in" Grid.Row="0" Click="Button_Click"/>
        <WebBrowser Name="webBrowser" Grid.Row="1" Navigated="webBrowser_Navigated"/>
    </Grid>
</Window>
using System.Linq;
using System.Windows;
using System.Windows.Navigation;
using Microsoft.Live;

namespace OneDriveTester
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }


        private LiveAuthClient liveAuthClient;
        private LiveConnectClient liveConnectClient;
        private readonly string clientId = "ここにクライアントIDを";        
        string[] scopes = new string[] { "wl.signin", "wl.skydrive" };


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.liveAuthClient = new LiveAuthClient(clientId);
            webBrowser.Navigate(this.liveAuthClient.GetLoginUrl(scopes));
        }


        private async void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {
            if (this.webBrowser.Source.AbsoluteUri.StartsWith("https://login.live.com/oauth20_desktop.srf"))
            {
                //認証後のurlからcodeパラメーターを取得
                string authenticationCode = this.webBrowser.Source
                                            .Query.TrimStart('?').Split('&')
                                            .Where(x => x.IndexOf("code=") == 0)
                                            .Single()
                                            .Substring(5);

                LiveConnectSession session = await this.liveAuthClient.ExchangeAuthCodeAsync(authenticationCode);
                this.liveConnectClient = new LiveConnectClient(session);
                LiveOperationResult meRs = await this.liveConnectClient.GetAsync("me");
                dynamic meData = meRs.Result;

                MessageBox.Show("Name: " + meData.name + "ID: " + meData.id);               
            }
        }
    }
}

19行目のscopesについては、スコープとアクセス許可に詳細がありました。

後は、LiveSDKのGitHubからサンプル落として走らせてみるのが手っ取り早いと思います。

Windowsで動くか試してないけど、OneDriveのファイル操作とかが書いてある場所を見つけたのでメモ。 Microsoft OneDrive のフォルダーとファイルの操作

WindowsPhoneでしか動作しないコードとかもあるのでデスクトップ環境でLiveSDK使う場合は気をつけましょう!

4423.ch