package main import ( "crypto/md5" "encoding/hex" "encoding/json" "fmt" "io/ioutil" "log" "net/http" "qfw/util" "strings" "time" ) var ( config map[string]string // appid = "jyNjdXQgUDAwdaTklMPz5i" // key = "404M0v2j" // apiurl = "http://127.0.0.1:8801" // appid = "jyMi1XQgMABQNcSkBMIhBq" // key = "6PzV0CUa" // apiurl = "https://testapi.jianyu360.com" appid, key, apiurl, day string ) func main() { util.ReadConfig(&config) appid = config["appid"] key = config["key"] apiurl = config["apiurl"] day = config["day"] getData() } func getToken() (token string) { tm := fmt.Sprintf("%d", time.Now().Unix()) res := post(apiurl+"/user/access_token", map[string]string{ "appid": appid, "timestamp": tm, "signature": MD5(appid + tm + key), //"key": "6PzV0CUa", }) log.Println(tm, MD5(appid+tm+key), res) if res != nil && res["access_token"] != "" { token, _ = res["access_token"].(string) } return } func getData() { token := getToken() data := post(apiurl+"/data/getalldata", map[string]string{ "access_token": token, "day": day, "next": "92", }) //s, _ := json.Marshal(data["data"]) //delete(data, "data") log.Println(token, data) } func MD5(str string) string { h := md5.New() h.Write([]byte(str)) return strings.ToUpper(hex.EncodeToString(h.Sum(nil))) } func post(url string, form map[string]string) (data map[string]interface{}) { str := "" for k, v := range form { str += "&" + k + "=" + v } log.Println(str) res, err := http.Post(url, "application/x-www-form-urlencoded", strings.NewReader(str)) if err != nil { log.Println("post err:", err.Error()) } else if res.Body != nil { defer res.Body.Close() bs, _ := ioutil.ReadAll(res.Body) json.Unmarshal(bs, &data) } return }