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)) }