Created
December 19, 2017 02:31
-
-
Save rakd/f334edb8b0f0e60929693dfdd7824e94 to your computer and use it in GitHub Desktop.
This file contains 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
package main | |
import ( | |
"crypto/hmac" | |
"crypto/sha1" | |
"time" | |
"encoding/hex" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"net/url" | |
"os" | |
"strings" | |
) | |
var fybsgKey, fybsgSecret string | |
func init() { | |
log.Print("fybsg.init") | |
fybsgKey = getEnvWithDefault("FYBSG_KEY", "") | |
fybsgSecret = getEnvWithDefault("FYBSG_SECRET", "") | |
if fybsgSecret == "" || fybsgKey == "" { | |
log.Fatal("fybsgKey & fybsgSecret must not be blank") | |
} | |
//log.Printf("fybsgSecret=%s", fybsgSecret) | |
//log.Printf("fybsgKey=%s", fybsgKey) | |
} | |
// getEnvWithDefault ... | |
func getEnvWithDefault(key string, def string) string { | |
v := os.Getenv(key) | |
if v == "" { | |
return def | |
} | |
return v | |
} | |
func generateHmacSha1(text, key string) string { | |
hasher := hmac.New(sha1.New, []byte(key)) | |
hasher.Write([]byte(text)) | |
return hex.EncodeToString(hasher.Sum(nil)) | |
} | |
func main() { | |
client := &http.Client{} | |
ts := time.Now().Unix() | |
values := url.Values{} | |
values.Add("timestamp", fmt.Sprintf("%d", ts)) | |
urlstr := "https://www.fybsg.com/api/SGD/getaccinfo" | |
req, err := http.NewRequest("POST", urlstr, strings.NewReader(values.Encode())) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
//log.Printf("generateHmacSha1=%s", generateHmacSha1(values.Encode(), fybsgSecret)) | |
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | |
req.Header.Add("key", fybsgKey) | |
req.Header.Add("sig", generateHmacSha1(values.Encode(), fybsgSecret)) | |
resp, err := client.Do(req) | |
if err != nil { | |
fmt.Println(err) | |
return | |
} | |
defer resp.Body.Close() | |
respBody, _ := ioutil.ReadAll(resp.Body) | |
fmt.Println(resp.Status) | |
fmt.Println(string(respBody)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment