第三方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.

api.go 1.8 KiB

5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package meituan_cps
  2. import (
  3. "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/comm"
  4. zhios_third_party_utils "code.fnuoos.com/go_rely_warehouse/zyos_go_third_party_api.git/utils"
  5. "crypto/hmac"
  6. "crypto/md5"
  7. "crypto/sha256"
  8. "encoding/base64"
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. func Send(method, key, secret string, param map[string]interface{}) (string, error) {
  14. header := map[string]string{
  15. "S-Ca-App": key,
  16. "S-Ca-Timestamp": zhios_third_party_utils.Int64ToStr(time.Now().Unix() * 1000),
  17. }
  18. url := "https://media.meituan.com/cps_open/common/api/v1/"
  19. paramStr := comm.GetSortJson(param)
  20. paramStr = strings.ReplaceAll(paramStr, "\n", "")
  21. header = GetSign(method, secret, paramStr, header)
  22. fmt.Println(url + method)
  23. fmt.Println(header)
  24. fmt.Println(paramStr)
  25. post, err := zhios_third_party_utils.CurlPost(url+method, paramStr, header)
  26. fmt.Println(string(post))
  27. fmt.Println(err)
  28. return string(post), err
  29. }
  30. func GetSign(method, secret string, paramStr string, header map[string]string) map[string]string {
  31. headers := comm.KsortToStr(header)
  32. HTTPMethod := "POST"
  33. h1 := md5.New()
  34. h1.Write([]byte(paramStr))
  35. ContentMd5 := base64.StdEncoding.EncodeToString(h1.Sum(nil))
  36. fmt.Println(ContentMd5)
  37. headerStr := ""
  38. signHeader := ""
  39. for _, v := range headers {
  40. headerStr += v + ":" + header[v] + "\n"
  41. if signHeader == "" {
  42. signHeader += v
  43. } else {
  44. signHeader += "," + v
  45. }
  46. }
  47. Url := "/cps_open/common/api/v1/" + method
  48. sign := HTTPMethod + "\n" + ContentMd5 + "\n" + headerStr + Url
  49. h := hmac.New(sha256.New, []byte(secret))
  50. _, _ = h.Write([]byte(sign))
  51. header["S-Ca-Signature"] = base64.StdEncoding.EncodeToString(h.Sum(nil))
  52. header["Content-MD5"] = ContentMd5
  53. header["S-Ca-Signature-Headers"] = signHeader
  54. header["Content-Type"] = "application/json; charset=utf-8"
  55. return header
  56. }