プラグインの登録
まだ何もメソッドの処理を記述していませんが,
プラグインの登録はレジストリを編集する必要があります。ここでは直接レジストリエディタ
キーの場所
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Live\PublishPlugins\[任意の名前]
値(いずれも文字列値)
名前 | 値 |
---|---|
AssemblyPath | DLLファイルへの絶対パス |
ClassName | 名前空間を含めたプラグインのクラス名 |
FriendlyName | Liveアプリケーションのメニューに表示される名前 |
追加した後,
デバッグに関してですが,
- ※3
スタートメニューからファイル名を指定して実行を選択しregeditと入力するとエディタが起動します。必要のないところまで変更しないよう編集には十分注意してください。
- ※4
Vista 64bit版の場合,
HKEY_ LOCAL_ MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows Live\PublishPluginsが対象のキーとなります。
はてなフォトライフAPIの利用
はてなフォトライフに写真をアップロードするコードは前回の記事中に書いています。前回のコードに少し変更を加えてプラグインクラスに追加します。
前回作成したメソッドは次の3個でした。
- CreateWsseHeaderValue
- GetPostUri
- PostPhoto
そして,
Imports <xmlns="http://purl.org/atom/ns#">
Imports <xmlns:hatena="http://www.hatena.ne.jp/info/xmlns#">
Imports <xmlns:dc="http://purl.org/dc/elements/1.1/">
プラグインでもXMLを扱うためデフォルトの名前空間を次のように変更します。
Imports <xmlns:atom="http://purl.org/atom/ns#"> ' 変更
Imports <xmlns:hatena="http://www.hatena.ne.jp/info/xmlns#">
Imports <xmlns:dc="http://purl.org/dc/elements/1.1/">
この変更に併せてGetPostUriメソッドの以下の部分を変更します。
' <link rel="service.post">要素の取得
' 以下へ変更 Dim elements = From e In document.<feed>.<link> Where e.@rel = "service.post"
Dim elements = From e In document.<atom:feed>.<atom:link> Where e.@rel = "service.post"
PostPhotoメソッドは少し引数が多くなりますが,
Protected Shared Sub PostPhoto(ByVal userName As String, ByVal password As String, _
ByVal postUri As String, ByVal title As String, ByVal contentType As String, ByVal photoStream As System.IO.Stream)
' リクエストの作成
Dim request = DirectCast(WebRequest.Create(postUri), HttpWebRequest)
With request
.Method = "POST"
' WSSE認証用
.Headers.Add(HttpRequestHeader.Authorization, "WSSE profile=""UsernameToken""")
.Headers.Add("X-WSSE", CreateWsseHeaderValue(userName, password))
.ContentType = "application/x.atom+xml"
.Accept = "application/x.atom+xml, application/xml, text/xml, */*"
.ServicePoint.Expect100Continue = False
End With
' 写真をバイト配列として読み込み
Dim photoBuffer = New Byte() {}
Using reader = New System.IO.BinaryReader(photoStream)
If photoStream.Length <= Integer.MaxValue Then
photoBuffer = reader.ReadBytes(CInt(photoStream.Length))
End If
End Using
' <entry>要素
Dim entry = <entry xmlns="http://purl.org/atom/ns#">
<title><%= title %></title>
<content mode="base64" type=<%= contentType %>><%= Convert.ToBase64String(photoBuffer) %></content>
<hatena:generator url="http://gihyo.jp/dev/serial/01/wl-sdk/" version="1.0">gihyo.jp sample tool</hatena:generator>
</entry>
' <entry>要素をバイト配列へ変換
Dim entryBuffer = System.Text.Encoding.UTF8.GetBytes(entry.ToString)
request.ContentLength = entryBuffer.Length
' POSTデータ書き込み
Using stream = request.GetRequestStream
stream.Write(entryBuffer, 0, entryBuffer.Length)
End Using
' レスポンス取得
Dim document As XDocument ' (記事中では使用していません)
Using response = DirectCast(request.GetResponse, HttpWebResponse), _
reader = New System.IO.StreamReader(response.GetResponseStream, System.Text.Encoding.UTF8)
document = XDocument.Load(reader)
End Using
End Sub
作成するプラグインではアップロード先のフォルダを指定しませんので,
CreateWsseHeaderValueメソッドは変更せずに使用します。