package cn.feishu.sdk.provider; import cn.feishu.sdk.common.ApiConstant; import cn.feishu.sdk.util.HttpUtil; import cn.feishu.sdk.util.PropertiesUtils; import com.alibaba.fastjson.JSONObject; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URLEncoder; public class AuthProvider { public String getAuthUrl(String state) throws UnsupportedEncodingException, MalformedURLException { String redirectUri = PropertiesUtils.getConfig("callback_url"); return HttpUtil.feishuGetRequestString(ApiConstant.FEISHU_API_URL+ApiConstant.AUTH_INDEX+"?redirect_uri="+ URLEncoder.encode(redirectUri,"UTF-8")+"&app_id="+ApiConstant.FEISHU_APP_ID+"&state="+state); } public JSONObject getAccessToken(String code) throws Exception { JSONObject params = new JSONObject(); params.put("app_access_token",getAppAccessToken()); params.put("grant_type","authorization_code"); params.put("code",code); return HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.AUTH_ACCESS_TOKEN,params); } public JSONObject refreshAccessToken(String refreshToken) throws Exception{ JSONObject params = new JSONObject(); params.put("app_access_token",getAppAccessToken()); params.put("grant_type","refresh_token"); params.put("refresh_token",refreshToken); return HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.REFRESH_AUTH_ACCESS_TOKEN,params); } public JSONObject getUserInfo(String accessToken){ JSONObject params = new JSONObject(); params.put("user_access_token",accessToken); return HttpUtil.feishuGetRequest("Bearer user_access_token",ApiConstant.FEISHU_API_URL+ApiConstant.USER_INFO,params); } public String getAppAccessToken() throws Exception { JSONObject params = new JSONObject(); params.put("app_id",ApiConstant.FEISHU_APP_ID); params.put("app_secret",ApiConstant.FEISHU_APP_SECRET); JSONObject resultNode = HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.APP_ACCESS_TOKEN,params); if (resultNode.getIntValue("code")==0){ return resultNode.getString("app_access_token"); }else { throw new Exception(); } } public String getTenantAccessToken() throws Exception { JSONObject params = new JSONObject(); params.put("app_id",ApiConstant.FEISHU_APP_ID); params.put("app_secret",ApiConstant.FEISHU_APP_SECRET); JSONObject resultNode = HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.TENANT_ACCESS_TOKEN,params); if (resultNode.getIntValue("code")==0){ return resultNode.getString("tenant_access_token"); }else { throw new Exception(); } } }