アプリケーションコンタクト
Live Messenger Web Toolkitを利用するとLive Messengerと連携したSNSなどのWebサイトも作れます。メンバー管理やメッセージング機能はそのままLive Messengerのものを使用することができそうですね。しかし,
多くのSNSではメールアドレスを知らせずにメッセージのやりとりができ,
アプリケーションコンタクトはUI Controlsとして提供されています。用意されているコントロールは次のふたつです。
- Application Contactsコントロール
- Application Contactコントロール
前者のApplication Contactsコントロールは,
Messenger Application Keyの取得
アプリケーションコンタクトを使用するには,
Messenger Application Key取得には,
Messenger Application Keyは,
https://consent.messenger.services.live.com/applicationsettings.aspx?appid=0x{appid}
上記URLのサイトは図1のようになっています。このページでは,
アプリケーションコンタクトの作成
それでは,
Application Contactsコントロール
アプリケーションコンタクトを有効にするにはApplication Contactsコントロールを使用します
<msgr:app-contacts
contacts="..."
signature="..."
on-set-contacts-completed="...">
</msgr:app-contacts>
contacts属性には,
アプリケーションコンタクトリスト
アプリケーションコンタクトリストは,
<Ticket>
<Contact id="64801418014"/>
<Contact id="44801418141"/>
<Contact id="11447918014"/>
<Contact id="-35143418014"/>
<Contact id="-89571418014"/>
<Contact id="58711418014"/>
<TS>2010-07-20T12:34:56Z</TS>
</Ticket>
<Ticket>要素の中に,
このXML文書の文字列を先ほどのApplication Contactsコントロールのcontacts属性に指定します。指定する際は,
CIDの配列からアプリケーションコンタクトの属性値を生成するメソッド例を以下に示します。
Public Function CreateApplicationContactList(ByRef cids() As Long) As String
Dim contactCids =
For Each cid In cids
contactCids &= String.Format("<contact id="" {0}""="">", cid)
Next
Return String.Format("<ticket>{0}<ts>{1}</ts></ticket>", contactCids, DateTime.UtcNow.ToString("o"))
End Function
使用するときは,
' アプリケーションコンタクトリストのメンバー CID
Dim cids() As Long = New Long() {5566738540414425322, _
-6356285628991451204, _
-5771274640885475729}
' アプリケーションコンタクトリスト
Dim list = CreateApplicationContactList(cids)
アプリケーションコンタクトリストの署名
続いてsignature属性値の生成方法をみてみましょう。値は,
実は,
' Imports System.Security.Cryptography
Public Shared Function CreateSignature(ByVal messengerApplicationKey As String, ByVal applicationContactList As String) As String
Dim key = Derive(messengerApplicationKey, "SIGNATURE")
Dim hashAlgorithm = New HMACSHA256(key)
Dim data = System.Text.Encoding.Default.GetBytes(applicationContactList)
Return Convert.ToBase64String(hashAlgorithm.ComputeHash(data))
End Function
Public Shared Function Derive(ByVal secretKey As String, ByVal prefix As String) As Byte()
Dim hashAlgorithm = New SHA256Managed
Dim buffer = System.Text.Encoding.Default.GetBytes(prefix & secretKey)
Dim hashOutput = hashAlgorithm.ComputeHash(buffer)
Dim byteKey(15) As Byte
Array.Copy(hashOutput, byteKey, byteKey.Length)
Return byteKey
End Function
使用方法は以下の通りです。
' list: アプリケーションコンタクトリスト
Dim signature = CreateSignature("取得したMessengerApplicationKey", list)