第三方api接口
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 regels
2.5 KiB

  1. package face_check
  2. import (
  3. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  4. "crypto/hmac"
  5. "crypto/sha256"
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "sort"
  10. "time"
  11. )
  12. func Send(url string, appKey, appSecret string, arg, HeaderArr map[string]string) (string, error) {
  13. sign := Sign(url, arg, "POST", appKey, appSecret, HeaderArr)
  14. HeaderArr["X-Ca-Signature"] = sign
  15. domain := "https://zfah.market.alicloudapi.com"
  16. post, err := zhios_third_party_utils.CurlPost(domain+url, arg, HeaderArr)
  17. if err != nil {
  18. return "", err
  19. }
  20. if post == nil {
  21. return "", errors.New("获取数据失败")
  22. }
  23. fmt.Println(post)
  24. fmt.Println(err)
  25. return string(post), nil
  26. }
  27. func Sign(url string, arg map[string]string, HTTPMethod, appKey, appSecret string, header map[string]string) string {
  28. headerStr := make([]string, 0)
  29. for k := range header {
  30. headerStr = append(headerStr, k)
  31. }
  32. sort.Strings(headerStr)
  33. str := HTTPMethod + "\n"
  34. str += header["Accept"] + "\n"
  35. str += header["Content-MD5"] + "\n"
  36. str += header["Content-Type"] + "\n"
  37. str += header["Date"] + "\n"
  38. var filter = []string{"X-Ca-Signature", "X-Ca-Signature-Headers", "Accept", "Content-MD5", "Content-Type", "Date"}
  39. for _, v := range headerStr {
  40. if zhios_third_party_utils.InArr(v, filter) {
  41. continue
  42. }
  43. str += v + ":" + header[v] + "\n"
  44. }
  45. pathStr := ""
  46. argStr := make([]string, 0)
  47. for k := range arg {
  48. argStr = append(argStr, k)
  49. }
  50. sort.Strings(argStr)
  51. for _, v := range argStr {
  52. if pathStr == "" {
  53. pathStr += "?" + v + "=" + arg[v]
  54. } else {
  55. pathStr += "&" + v + "=" + arg[v]
  56. }
  57. }
  58. str += url + pathStr
  59. fmt.Println(str)
  60. hmacSHA256 := GenHmacSha256(str, appSecret)
  61. fmt.Println(hmacSHA256)
  62. return hmacSHA256
  63. }
  64. func GenHmacSha256(message string, secret string) string {
  65. h := hmac.New(sha256.New, []byte(secret))
  66. h.Write([]byte(message))
  67. //sha := hex.EncodeToString(h.Sum(nil))
  68. //fmt.Printf("sha:%s\n", sha)
  69. return Base64UrlSafeEncode(h.Sum(nil))
  70. }
  71. func Base64UrlSafeEncode(source []byte) string {
  72. byteArr := base64.StdEncoding.EncodeToString(source)
  73. return byteArr
  74. }
  75. func Header(appKey string) map[string]string {
  76. headers := map[string]string{
  77. "Accept": "application/json; charset=utf-8",
  78. "X-Ca-Key": appKey,
  79. "X-Ca-Nonce": zhios_third_party_utils.RandNum(),
  80. "X-Ca-Signature-Method": "HmacSHA256",
  81. "X-Ca-Timestamp": zhios_third_party_utils.Int64ToStr(time.Now().Unix() * 1000),
  82. "X-Ca-Signature-Headers": "X-Ca-Key,X-Ca-Nonce,X-Ca-Signature-Method,X-Ca-Timestamp",
  83. }
  84. return headers
  85. }