info.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package zoho
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "pms-api-go/api"
  7. "pms-api-go/pkg/lfshook"
  8. "github.com/gin-gonic/gin"
  9. "gopkg.in/ini.v1"
  10. )
  11. // @tags PBX-zoho
  12. // @Summary 获取token
  13. // @Description 获取token
  14. // @Security ZohoToken
  15. // @Accept json
  16. // @Produce json
  17. // @Router /api/zoho/gettoken [post]
  18. func getToken(ctx *gin.Context) {
  19. fmt.Printf("getToken ............\n")
  20. // 获取配置文件信息
  21. // confPath := "/etc/asterisk/vtiger_api.conf"
  22. confPath := "/etc/asterisk/pms_api.conf"
  23. cfg, err := ini.Load(confPath)
  24. if err != nil {
  25. lfshook.NewLogger().Error(err)
  26. return
  27. }
  28. ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  29. ZohoCode := cfg.Section("general").Key("zohoCode").String()
  30. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  31. ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
  32. if ZohoAuthUrl == "" || ZohoCode == "" || ZohoClientId == "" || ZohoClientSecret == "" {
  33. lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set zohoAuthUrl or zohoCode or zohoClientId or zohoClientSecret")
  34. return
  35. }
  36. // 创建HTTP客户端
  37. client := &http.Client{}
  38. // 创建请求
  39. // https://accounts.zoho.com/oauth/v3/device/token?code=1004.d4c145db9ec33e64f955290f0905ff1e.eebc39b33f228c3bee806d6f8200c50f
  40. // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=device_token
  41. getURL := fmt.Sprintf("%s/oauth/v3/device/token?code=%s&client_id=%s&client_secret=%s&grant_type=device_token", ZohoAuthUrl, ZohoCode, ZohoClientId, ZohoClientSecret)
  42. fmt.Printf("getURL = %s\n", getURL)
  43. req, err := http.NewRequest("POST", getURL, nil)
  44. if err != nil {
  45. fmt.Println("创建请求时发生错误:", err)
  46. return
  47. }
  48. // req.Header.Set("Authorization", "Bearer "+accessToken) // 获取token时不需要
  49. // 发送请求
  50. resp, err := client.Do(req)
  51. if err != nil {
  52. fmt.Println("发送请求时发生错误:", err)
  53. return
  54. }
  55. defer resp.Body.Close()
  56. // 读取请求后的响应
  57. data, err := ioutil.ReadAll(resp.Body)
  58. if err != nil {
  59. fmt.Println("读取请求后的响应时发生错误:", err)
  60. return
  61. }
  62. // 打印请求后的响应
  63. fmt.Printf("data = %+v\n", string(data))
  64. api.Success(ctx, string(data))
  65. }
  66. // @tags PBX-zoho
  67. // @Summary 刷新token
  68. // @Description 刷新token
  69. // @Security ZohoToken
  70. // @Accept json
  71. // @Produce json
  72. // @Router /api/zoho/refresh-token [post]
  73. func refreshToken(ctx *gin.Context) {
  74. fmt.Printf("refreshToken ............\n")
  75. // 获取配置文件信息
  76. // confPath := "/etc/asterisk/vtiger_api.conf"
  77. confPath := "/etc/asterisk/pms_api.conf"
  78. cfg, err := ini.Load(confPath)
  79. if err != nil {
  80. lfshook.NewLogger().Error(err)
  81. return
  82. }
  83. ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
  84. ZohoRefreshToken := cfg.Section("general").Key("zohoRefreshToken").String()
  85. ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
  86. ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
  87. if ZohoAuthUrl == "" || ZohoRefreshToken == "" || ZohoClientId == "" || ZohoClientSecret == "" {
  88. lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set zohoAuthUrl or zohoRefreshToken or zohoClientId or zohoClientSecret")
  89. return
  90. }
  91. // 创建HTTP客户端
  92. client := &http.Client{}
  93. // 创建请求
  94. // https://accounts.zoho.com/oauth/v2/token?refresh_token=1004.86c8c0e3db7bfe9133598825bef28eb9.17a82a3bf3e675c504f478c1b0b5c456
  95. // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=refresh_token
  96. getURL := fmt.Sprintf("%s/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", ZohoAuthUrl, ZohoRefreshToken, ZohoClientId, ZohoClientSecret)
  97. fmt.Printf("getURL = %s\n", getURL)
  98. req, err := http.NewRequest("POST", getURL, nil)
  99. if err != nil {
  100. fmt.Println("创建请求时发生错误:", err)
  101. return
  102. }
  103. // req.Header.Set("Authorization", "Bearer "+accessToken) // 刷新token时不需要
  104. // 发送请求
  105. resp, err := client.Do(req)
  106. if err != nil {
  107. fmt.Println("发送请求时发生错误:", err)
  108. return
  109. }
  110. defer resp.Body.Close()
  111. // 读取请求后的响应
  112. data, err := ioutil.ReadAll(resp.Body)
  113. if err != nil {
  114. fmt.Println("读取请求后的响应时发生错误:", err)
  115. return
  116. }
  117. // 打印请求后的响应
  118. fmt.Printf("data = %+v\n", string(data))
  119. api.Success(ctx, string(data))
  120. }