component_token.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /**
  2. * Copyright (c) 2022 Yansen Zhang
  3. * wxcomponent is licensed under Mulan PSL v2.
  4. * You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. * You may obtain a copy of Mulan PSL v2 at:
  6. * http://license.coscl.org.cn/MulanPSL2
  7. * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. * See the Mulan PSL v2 for more details.
  11. **/
  12. package authorization
  13. import (
  14. "encoding/json"
  15. "gitee.com/yansen_zh/wxcomponent/config"
  16. "gitee.com/yansen_zh/wxcomponent/errors"
  17. "gitee.com/yansen_zh/wxcomponent/utils/request"
  18. )
  19. // ComponentTokenResult 获取令牌结果
  20. type ComponentTokenResult struct {
  21. errors.Error
  22. ComponentAccessToken string `json:"component_access_token"`
  23. ExpiresIn int `json:"expires_in"`
  24. }
  25. const (
  26. apiComponentToken = "https://apies.weixin.qq.com/cgi-bin/component/api_component_token"
  27. )
  28. // GetComponentToken 获取令牌
  29. func GetComponentToken() (*ComponentTokenResult, error) {
  30. data := map[string]string{
  31. "component_appid": config.GetAppID(),
  32. "component_appsecret": config.GetAppSecret(),
  33. "component_verify_ticket": config.GetVerifyTicket(),
  34. }
  35. resp, e2 := request.PostJSON(apiComponentToken, nil, data)
  36. if e2 != nil {
  37. return nil, e2
  38. }
  39. result := ComponentTokenResult{}
  40. if err := json.Unmarshal(resp, &result); err != nil {
  41. return nil, err
  42. }
  43. return &result, nil
  44. }