サンプルアプリの作成
それでは,
サンプルアプリは,
今回のサンプルアプリは,
それでは,
サインインウィンドウ
最初に,
新しい項目の追加から,
SignInWindow.
<Window x:Class="SignInWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="サインイン" Height="450" Width="560">
<Grid>
<WebBrowser x:Name="SignInWebBrowser" />
</Grid>
</Window>
SignInWindow.
Private ClientId As String
Private Scopes As New List(Of String)
Public Sub New(clientId As String, scope As IEnumerable(Of String))
Me.ClientId = clientId
Me.Scopes.AddRange(scope)
InitializeComponent()
End Sub
ウィンドウを開いた時にWebブラウザーコントロールに認証・
Private Const AuthorizeUriFormat As String = "https://oauth.live.com/authorize?client_id={0}&scope={1}&response_type=token&locale=ja&display=popup&redirect_uri=https://oauth.live.com/desktop"
Private Sub SignInWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
SignInWebBrowser.Navigate(
String.Format(AuthorizeUriFormat, Me.ClientId, String.Join("+", Me.Scopes)))
End Sub
サインインウィンドウ クラスに,
Private _AccessToken As String
ReadOnly Property AccessToken As String
Get
Return _AccessToken
End Get
End Property
ユーザーが認可処理を終えると,
Private Sub SignInWebBrowser_Navigated(sender As System.Object, e As System.Windows.Navigation.NavigationEventArgs) Handles SignInWebBrowser.Navigated
' 表示しているページの URL が https://oauth.live.com/desktop でなければ処理を抜ける
Dim redirectUri = New Uri("https://oauth.live.com/desktop")
If e.Uri.Host <> redirectUri.Host OrElse
e.Uri.AbsolutePath <> redirectUri.AbsolutePath Then
Exit Sub
End If
' URL のフラグメント部分から access_token の値を取得する
If e.Uri.Fragment.Contains("access_token") Then
Dim items = New Dictionary(Of String, String)
Dim param = e.Uri.Fragment.Substring(1).Split({"&"c, "="c})
For i = 0 To param.Length - 1 Step 2
items.Add(param(i), param(i + 1))
Next
If items.ContainsKey("access_token") Then
' URL に access_token が含まれていた場合
_AccessToken = items("access_token")
Me.DialogResult = True
End If
End If
' サインインウィンドウを閉じる
Me.Close()
End Sub
最後にウィンドウを閉じて完了です。以上で,