AuthProvider.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package cn.feishu.sdk.provider;
  2. import cn.feishu.sdk.common.ApiConstant;
  3. import cn.feishu.sdk.util.HttpUtil;
  4. import cn.feishu.sdk.util.PropertiesUtils;
  5. import com.alibaba.fastjson.JSONObject;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.MalformedURLException;
  8. import java.net.URLEncoder;
  9. public class AuthProvider {
  10. public String getAuthUrl(String state) throws UnsupportedEncodingException, MalformedURLException {
  11. String redirectUri = PropertiesUtils.getConfig("callback_url");
  12. 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);
  13. }
  14. public JSONObject getAccessToken(String code) throws Exception {
  15. JSONObject params = new JSONObject();
  16. params.put("app_access_token",getAppAccessToken());
  17. params.put("grant_type","authorization_code");
  18. params.put("code",code);
  19. return HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.AUTH_ACCESS_TOKEN,params);
  20. }
  21. public JSONObject refreshAccessToken(String refreshToken) throws Exception{
  22. JSONObject params = new JSONObject();
  23. params.put("app_access_token",getAppAccessToken());
  24. params.put("grant_type","refresh_token");
  25. params.put("refresh_token",refreshToken);
  26. return HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.REFRESH_AUTH_ACCESS_TOKEN,params);
  27. }
  28. public JSONObject getUserInfo(String accessToken){
  29. JSONObject params = new JSONObject();
  30. params.put("user_access_token",accessToken);
  31. return HttpUtil.feishuGetRequest("Bearer user_access_token",ApiConstant.FEISHU_API_URL+ApiConstant.USER_INFO,params);
  32. }
  33. public String getAppAccessToken() throws Exception {
  34. JSONObject params = new JSONObject();
  35. params.put("app_id",ApiConstant.FEISHU_APP_ID);
  36. params.put("app_secret",ApiConstant.FEISHU_APP_SECRET);
  37. JSONObject resultNode = HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.APP_ACCESS_TOKEN,params);
  38. if (resultNode.getIntValue("code")==0){
  39. return resultNode.getString("app_access_token");
  40. }else {
  41. throw new Exception();
  42. }
  43. }
  44. public String getTenantAccessToken() throws Exception {
  45. JSONObject params = new JSONObject();
  46. params.put("app_id",ApiConstant.FEISHU_APP_ID);
  47. params.put("app_secret",ApiConstant.FEISHU_APP_SECRET);
  48. JSONObject resultNode = HttpUtil.feishuPostRequest(ApiConstant.FEISHU_API_URL+ApiConstant.TENANT_ACCESS_TOKEN,params);
  49. if (resultNode.getIntValue("code")==0){
  50. return resultNode.getString("tenant_access_token");
  51. }else {
  52. throw new Exception();
  53. }
  54. }
  55. }