123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- package zoho
- import (
- "fmt"
- "io/ioutil"
- "net/http"
- "pms-api-go/api"
- "pms-api-go/pkg/lfshook"
- "github.com/gin-gonic/gin"
- "gopkg.in/ini.v1"
- )
- // @tags PBX-zoho
- // @Summary 获取token
- // @Description 获取token
- // @Security ZohoToken
- // @Accept json
- // @Produce json
- // @Router /api/zoho/gettoken [post]
- func getToken(ctx *gin.Context) {
- fmt.Printf("getToken ............\n")
- // 获取配置文件信息
- // confPath := "/etc/asterisk/vtiger_api.conf"
- confPath := "/etc/asterisk/pms_api.conf"
- cfg, err := ini.Load(confPath)
- if err != nil {
- lfshook.NewLogger().Error(err)
- return
- }
- ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
- ZohoCode := cfg.Section("general").Key("zohoCode").String()
- ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
- ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
- if ZohoAuthUrl == "" || ZohoCode == "" || ZohoClientId == "" || ZohoClientSecret == "" {
- lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set zohoAuthUrl or zohoCode or zohoClientId or zohoClientSecret")
- return
- }
- // 创建HTTP客户端
- client := &http.Client{}
- // 创建请求
- // https://accounts.zoho.com/oauth/v3/device/token?code=1004.d4c145db9ec33e64f955290f0905ff1e.eebc39b33f228c3bee806d6f8200c50f
- // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=device_token
- getURL := fmt.Sprintf("%s/oauth/v3/device/token?code=%s&client_id=%s&client_secret=%s&grant_type=device_token", ZohoAuthUrl, ZohoCode, ZohoClientId, ZohoClientSecret)
- fmt.Printf("getURL = %s\n", getURL)
- req, err := http.NewRequest("POST", getURL, nil)
- if err != nil {
- fmt.Println("创建请求时发生错误:", err)
- return
- }
- // req.Header.Set("Authorization", "Bearer "+accessToken) // 获取token时不需要
- // 发送请求
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("发送请求时发生错误:", err)
- return
- }
- defer resp.Body.Close()
- // 读取请求后的响应
- data, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("读取请求后的响应时发生错误:", err)
- return
- }
- // 打印请求后的响应
- fmt.Printf("data = %+v\n", string(data))
- api.Success(ctx, string(data))
- }
- // @tags PBX-zoho
- // @Summary 刷新token
- // @Description 刷新token
- // @Security ZohoToken
- // @Accept json
- // @Produce json
- // @Router /api/zoho/refresh-token [post]
- func refreshToken(ctx *gin.Context) {
- fmt.Printf("refreshToken ............\n")
- // 获取配置文件信息
- // confPath := "/etc/asterisk/vtiger_api.conf"
- confPath := "/etc/asterisk/pms_api.conf"
- cfg, err := ini.Load(confPath)
- if err != nil {
- lfshook.NewLogger().Error(err)
- return
- }
- ZohoAuthUrl := cfg.Section("general").Key("zohoAuthUrl").String()
- ZohoRefreshToken := cfg.Section("general").Key("zohoRefreshToken").String()
- ZohoClientId := cfg.Section("general").Key("zohoClientId").String()
- ZohoClientSecret := cfg.Section("general").Key("zohoClientSecret").String()
- if ZohoAuthUrl == "" || ZohoRefreshToken == "" || ZohoClientId == "" || ZohoClientSecret == "" {
- lfshook.NewLogger().Error("/etc/asterisk/pms_api.conf not set zohoAuthUrl or zohoRefreshToken or zohoClientId or zohoClientSecret")
- return
- }
- // 创建HTTP客户端
- client := &http.Client{}
- // 创建请求
- // https://accounts.zoho.com/oauth/v2/token?refresh_token=1004.86c8c0e3db7bfe9133598825bef28eb9.17a82a3bf3e675c504f478c1b0b5c456
- // &client_id=1004.LWJCJZD5O9DB6SZLL5YJEWHT7LH0BV&client_secret=fc3aef43dc58af8a49d3ed597710924200b03f74d0&grant_type=refresh_token
- getURL := fmt.Sprintf("%s/oauth/v2/token?refresh_token=%s&client_id=%s&client_secret=%s&grant_type=refresh_token", ZohoAuthUrl, ZohoRefreshToken, ZohoClientId, ZohoClientSecret)
- fmt.Printf("getURL = %s\n", getURL)
- req, err := http.NewRequest("POST", getURL, nil)
- if err != nil {
- fmt.Println("创建请求时发生错误:", err)
- return
- }
- // req.Header.Set("Authorization", "Bearer "+accessToken) // 刷新token时不需要
- // 发送请求
- resp, err := client.Do(req)
- if err != nil {
- fmt.Println("发送请求时发生错误:", err)
- return
- }
- defer resp.Body.Close()
- // 读取请求后的响应
- data, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- fmt.Println("读取请求后的响应时发生错误:", err)
- return
- }
- // 打印请求后的响应
- fmt.Printf("data = %+v\n", string(data))
- api.Success(ctx, string(data))
- }
|