XojoからCURL経由でMovable TypeのData APIを叩く。
公開日 : 2014-01-18 09:54:38
XojoからCURL経由でMovable TypeのData APIを叩く時のポイントなど。
JSONにセットする前に特定文字列はUnicodeエスケープシーケンスに変換する
sub RawDataEscape( Param As String )
Param = ReplaceAll( Param, "\", "\\" )
Param = ReplaceAll( Param, Chr( 34 ), "\u0022" )
Param = ReplaceAll( Param, Chr( 10 ), "\u000a" )
Param = ReplaceAll( Param, "<", "\u003C" )
Param = ReplaceAll( Param, ">", "\u003E" )
Param = ReplaceAll( Param, "&", "\u0026" )
Param = ReplaceAll( Param, "'", "\u0027" )
Param = ReplaceAll( Param, ";", "\u003B" )
Return Param
end sub
コマンドに渡す直前にJSON文字列のダブルコーテーションをエスケープする
sub JSONEscape( Param As String )
Param = ReplaceAll( Param, Chr( 34 ), "\" + Chr( 34 ) )
Return Param
end sub
Shellに渡す文字列をエスケープする
sub ShellEscape( Param As String )
Param = ReplaceAll( Param, " ", "\ " )
Param = ReplaceAll( Param, ";", "\;" )
Return Param
end sub
CURLコマンドには -s オプションを付ける
付けなくてもいいけども、JSONがそのまま受け取れるので。
sub BlogPost( Title,Body,EditEntryId As String )
Dim j as new JSONItem
j.Value( "title" ) = App.RawDataEscape( Title )
j.Value( "body" ) = App.RawDataEscape( Body )
j.Value( "status" ) = "Publish"
Dim Entry,Cmd As String
Entry = App.JSONEscape( j.ToString )
Cmd = "curl -s -H " + Chr( 34 ) + "X-MT-Authorization: MTAuth accessToken=" + App.AccessToken
Cmd = Cmd + Chr( 34 ) + " -d entry=" + Chr( 34 ) + Entry + Chr( 34 ) + " "
If EditEntryId <> "0" Then
Cmd = Cmd + " -d __method=" + Chr( 34 ) + "PUT" + Chr( 34 ) + " "
End If
Cmd = Cmd + APIField.Text + "/" + VersionField.Text + "/sites/" + BlogIDField.Text + "/entries"
If EditEntryId <> "0" Then
Cmd = Cmd + "/" + EditEntryId
End If
Dim Sh As New Shell
Sh.Execute( Cmd )
Dim Result As String
Result= Sh.ReadAll
//...
end sub
複数のパラメタをGETで渡す場合は & を \ でescapeする
sub GetEntries()
Dim Cmd As String
Cmd = "curl -s -H " + Chr( 34 ) + "X-MT-Authorization: MTAuth accessToken=" + App.AccessToken
Cmd = Cmd + Chr( 34 ) + " "
Cmd = Cmd + APIField.Text + "/" + VersionField.Text + "/sites/" + BlogIDField.Text + "/entries?limit=" + LimitField.Text
Cmd = Cmd + "\&fields=title,updatable,id,status,date,body,permalink"
Dim Sh As new Shell
Sh.Execute( Cmd )
//...
Dim Result As String
Result= Sh.ReadAll
end sub