Last active
June 25, 2018 13:23
-
-
Save masahide/cf916a54f2234cd087db to your computer and use it in GitHub Desktop.
Cybozu ガルーン API を golang で叩いてみる ref: http://qiita.com/yamasaki-masahide/items/03dfa6cd70ff20607b58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const SOAPRequest = `<?xml version="1.0" encoding="UTF-8"?> | |
| <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope" | |
| xmlns:xsd="http://www.w3.org/2001/XMLSchema" | |
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
| xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" | |
| xmlns:base_services="http://wsdl.cybozu.co.jp/base/2008"> | |
| <SOAP-ENV:Header> | |
| <Action SOAP-ENV:mustUnderstand="1" xmlns="http://schemas.xmlsoap.org/ws/2003/03/addressing">{{.Action}}</Action> | |
| <Security xmlns:wsu="http://schemas.xmlsoap.org/ws/2002/07/utility" | |
| SOAP-ENV:mustUnderstand="1" | |
| xmlns="http://schemas.xmlsoap.org/ws/2002/12/secext"> | |
| <UsernameToken wsu:Id="id"><Username>{{.Username}}</Username><Password>{{.Password}}</Password></UsernameToken> | |
| </Security> | |
| <Timestamp SOAP-ENV:mustUnderstand="1" Id="id" | |
| xmlns="http://schemas.xmlsoap.org/ws/2002/07/utility"> | |
| <Created>2037-08-12T14:45:00Z</Created> | |
| <Expires>2037-08-12T14:45:00Z</Expires> | |
| </Timestamp> | |
| <Locale>jp</Locale> | |
| </SOAP-ENV:Header><SOAP-ENV:Body> | |
| <{{.Action}}> | |
| {{.Parameters}} | |
| </{{.Action}}> | |
| </SOAP-ENV:Body></SOAP-ENV:Envelope> | |
| ` | |
| type APIParameters struct { | |
| Username, Password, Action, Parameters string | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func main() { | |
| var api_parameters = APIParameters{"ID", "パスワード", "ScheduleGetEvents", `<parameters start="2014-01-01T08:00:00" end="2014-01-30T20:00:00" ></parameters>`} | |
| var url = "https://ほげほげほげ/grn.cgi/cbpapi/schedule/api?" | |
| //リクエストbodyのテンプレートを適用 | |
| t := template.Must(template.New("SOAPRequest").Parse(SOAPRequest)) | |
| body := bytes.NewBufferString("") | |
| err := t.Execute(body, api_parameters) | |
| if err != nil { | |
| log.Panicf("template.Execute: %#v", err) | |
| } | |
| // Postする | |
| response, err := http.Post(url, "text/xml; charset=utf-8", body) | |
| if err != nil { | |
| log.Panicf("http.Post: %#v", err) | |
| } | |
| //レスポンスを読み込んで表示 | |
| b, err := ioutil.ReadAll(response.Body) | |
| response.Body.Close() | |
| if err != nil { | |
| log.Panicf("ioutil.ReadAll: %#v", err) | |
| } | |
| fmt.Printf(string(b)) | |
| return | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment