Java如何调用芝麻信用分?

来源:互联网 发布:全民奇迹成就进阶数据 编辑:程序博客网 时间:2024/04/28 19:25
首先我觉得你肯定要去芝麻信用分官网去流量一遍,看下流程,相关参数。
https://b.zmxy.com.cn/technology/openDoc.htm?relInfo=zhima.credit.score.get@1.0@1.4&relType=API_DOC&LEFT_MENU_MODE=null
 
可以看出来相关重要的值,公私钥,相关服务ID。然后根据官网提供的SDK,示例代码,基本就可以完成。
 
个人总结的资料以及代码业务文档(芝麻分):
链接:  https://pan.baidu.com/s/1hslY70w  密码: yc8k
 
个人总记的代码示例~~~~
 
  1. import java.net.URLDecoder;
  2.  
  3. import java.util.HashMap;
  4.  
  5. import java.util.Map;
  6.  
  7. import java.util.UUID;
  8.  
  9.  
  10.  
  11.  
  12. import com.alibaba.fastjson.JSON;
  13.  
  14. import com.alibaba.fastjson.JSONObject;
  15.  
  16. import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
  17.  
  18. import com.antgroup.zmxy.openplatform.api.ZhimaApiException;
  19.  
  20. import com.antgroup.zmxy.openplatform.api.internal.util.RSACoderUtil;
  21.  
  22. import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthorizeRequest;
  23.  
  24. import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthqueryRequest;
  25.  
  26. import com.antgroup.zmxy.openplatform.api.request.ZhimaCreditScoreGetRequest;
  27.  
  28. import com.antgroup.zmxy.openplatform.api.response.ZhimaAuthInfoAuthqueryResponse;
  29.  
  30. import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditScoreGetResponse;
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38. public class Test {
  39.  
  40.     
  41.  
  42.     //芝麻开放平台地址
  43.  
  44.     private static final String URL  = "https://zmopenapi.zmxy.com.cn/openapi.do";
  45.  
  46.     //商户应用 Id
  47.  
  48.     private static final  String APPID  = "";
  49.  
  50.     //商户 RSA 私钥
  51.  
  52.     private static final  String PRIKEY  = "";
  53.  
  54.     //芝麻 RSA 公钥
  55.  
  56.     private static final  String PUBKEY = "";
  57.  
  58.     //统一字符集
  59.  
  60.     private static final String CHARSET  = "UTF-8";
  61.  
  62.     
  63.  
  64.     /**
  65.  
  66.      * 查询授权信息
  67.  
  68.      */
  69.  
  70.     public void testZhimaAuthInfoAuthorize() {
  71.  
  72.         ZhimaAuthInfoAuthorizeRequest req = new ZhimaAuthInfoAuthorizeRequest();
  73.  
  74.         req.setIdentityType("2");// 身份标识
  75.  
  76.         req.setChannel("apppc"); // PC端
  77.  
  78.         //必要参数 state: 用于给商户提供透传的参数,芝麻会将此参数透传给商户;
  79.  
  80.         req.setBizParams("{\"auth_code\":\"M_APPPC_CERT\",\"state\":\"100111211\"}");
  81.  
  82.         req.setIdentityParam("{\"certNo\":\"330621198710114617\",\"certType\":\"IDENTITY_CARD\",\"name\":\"陈金赛\"}");// 必要参数        
  83.  
  84.         DefaultZhimaClient client = new DefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
  85.  
  86.         try {
  87.  
  88.             String url = client.generatePageRedirectInvokeUrl(req);
  89.  
  90.             System.out.println(url);
  91.  
  92.         } catch (ZhimaApiException e) {
  93.  
  94.             e.printStackTrace();
  95.  
  96.         }
  97.  
  98.     }
  99.  
  100.  
  101.  
  102.  
  103.     /**
  104.  
  105.      * 获取目标用户的open_id,没有openid,走授权
  106.  
  107.      */
  108.  
  109.     public void testZhimaAuthInfoReq() {
  110.  
  111.         ZhimaAuthInfoAuthqueryRequest req = new ZhimaAuthInfoAuthqueryRequest();
  112.  
  113.      // 0:芝麻信用开放账号ID 1:按照手机号进行授权 2:按照身份证+姓名进行授权 3通过公安网验证进行授权 4.通过人脸验证进行授权
  114.  
  115.         req.setIdentityType("2");        
  116.  
  117.         req.setIdentityParam("{\"certNo\":\"522121198710114617\",\"certType\":\"IDENTITY_CARD\",\"name\":\"但镜宇\"}");// 必要参数        
  118.  
  119.         DefaultZhimaClient client = new DefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
  120.  
  121.         try {
  122.  
  123.             // 如果正常返回,直接在对象里面获取结果值
  124.  
  125.             ZhimaAuthInfoAuthqueryResponse response = client.execute(req);
  126.  
  127.             System.out.println(JSON.toJSON(response));
  128.  
  129. /*
  130.  
  131.  成功响应报文
  132.  
  133.  {
  134.  
  135.                  "authorized": true,
  136.  
  137.                   "body": "{\"success\":true,\"authorized\":true,\"open_id\":\"268816231939676969685782895\"}",
  138.  
  139.                   "openId": "268816231939676969685782895",
  140.  
  141.                   "params": {
  142.  
  143.                 "params": "faeSa69w3kOL+ZRAuHykvufu6ogiqxA48l5hzPutKItPy/2HKtdznbzvLhutsY6KTqLgwS/Af1GN1Y7MLh+XDFtjVUf5kF/npUspRkyFUCPpASJAMxeK56vaELnZ4yCVFjVseOBYlCn4YOCZWmBpu02l7IctE6ZP3r1o4HUR+sMQYlYu2xFHfXiqX2g0vdQn81pJ/fcQ4OuBzq1d3c+qniBXf3iESni1m21nMjxeUclzCdZwXWCI1aIYQnDFgVo/58kJ9CTw/6FrffUcP+c6zvzdM8RNCefiGGuF5Vef+C6tck1kBaYoYT6BS5u4ahYteoiG+XJgzLKF+BqAplhq/A=="
  144.  
  145.                   },
  146.  
  147.                   "success": true
  148.  
  149.     }
  150.  
  151. // 
  152.  
  153.   失败响应报文
  154.  
  155.            {
  156.  
  157.                   "authorized": false,
  158.  
  159.                   "body": "{\"success\":true,\"authorized\":false}",
  160.  
  161.                   "params": {
  162.  
  163.                 "params": "lGYId9CaU8McGDd8U0ZMM8Pky1/ulOE44LkgGcrWrdzmEtU1JBT5M+Jjd5e27bz45d/CNEqbwmwtg3VsozZTol42YKNi+MYzvvyZEOsUT9F68qiIPxxlOv3vGJsUpB+LJMOI8ZU7eXtHoCnbarQSOvtSylilp6dpIbAIYxbPPvSkj+7f1TiSne/7Re/XZ1qVVM18yTJyleCr+WlOe7o59lZAoiZ5bDoP2ta8MIjwlxz4/1d+IYe0f2BoZQa/9oDqJZ0MNQ0iPCbhqBKRuShPCQe8afEyOTTI0p+cROjwWTz0dwP7QXnkWBvzX3hrWXImdk7JszOVoSxAAAfwcVXNxw=="
  164.  
  165.               },
  166.  
  167.               "success": true
  168.  
  169.           }
  170.  
  171. */
  172.  
  173.         } catch (ZhimaApiException e) {
  174.  
  175.             e.printStackTrace();
  176.  
  177.         }
  178.  
  179.     }
  180.  
  181.     
  182.  
  183.     /**
  184.  
  185.      * 测试查询芝麻分.
  186.  
  187.      * @throws ZhimaApiException 
  188.  
  189.      */
  190.  
  191.     public static void testQueryScore() throws ZhimaApiException {
  192.  
  193.         ZhimaCreditScoreGetRequest creditScoreGetRequest = new ZhimaCreditScoreGetRequest();
  194.  
  195.         creditScoreGetRequest.setPlatform("zmop"); // 开放平台,zmop代表芝麻开放平台
  196.  
  197.         creditScoreGetRequest.setChannel("apppc"); // pc端
  198.  
  199.         //transactionId,该标记是商户每次请求的唯一标识。建议使用uuid进行传递,
  200.  
  201.         creditScoreGetRequest.setTransactionId(UUID.randomUUID().toString());
  202.  
  203.         creditScoreGetRequest.setProductCode("w1010100100000000001"); // 商户配置那块儿的产品Code
  204.  
  205.         creditScoreGetRequest.setOpenId("268816113399909561399995894"); // appid,每个人的标识
  206.  
  207.         DefaultZhimaClient client = new DefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
  208.  
  209.         // 如果正常返回,直接在对象里面获取结果值
  210.  
  211.         ZhimaCreditScoreGetResponse creditScoreGetResponse = client.execute(creditScoreGetRequest);
  212.  
  213.         System.out.println(JSON.toJSON(creditScoreGetResponse));
  214.  
  215.         
  216.  
  217. /*
  218.  
  219.     成功查询响应报文
  220.  
  221.    {
  222.  
  223.   "bizNo": "ZM20160120014501763890a37b16614532255017521309",
  224.  
  225.   "body": "{\"success\":true,\"biz_no\":\"ZM20160120014501763890a37b16614532255017521309\",\"zm_score\":751}",
  226.  
  227.   "params": {
  228.  
  229.     "params": "aqiMCUg1VbZRqflPyVyLKgxShcvc3WoWLTFewqA9PvQjp6yuC99lYlGdr/Bf2FevQ2zemdPTixqQWnO7qdz0pT2x4VFWVJdldPgLVsRr37qWDcO5NjvNZzOuEYozzGSnzwhTyelqglAdzOOepM0ows+7sNVJriABtRdt1HH8gqc="
  230.  
  231.   },
  232.  
  233.   "success": true,
  234.  
  235.   "zmScore": "751"
  236.  
  237.   }
  238.  
  239.   失败查询响应报文
  240.  
  241.   {
  242.  
  243.   "body": "{\"success\":false,\"error_code\":\"ZMCREDIT.api_product_not_match\",\"error_message\":\"输入的产品码不正确\"}",
  244.  
  245.   "errorCode": "ZMCREDIT.api_product_not_match",
  246.  
  247.   "errorMessage": "输入的产品码不正确",
  248.  
  249.   "params": {
  250.  
  251.     "params": "vJTcbtVvo0/WEG01Flve0//EbE70clk7v84B7MVJZH59/HefdyAZ1JG4opTna5ANWvY8UArsGlVIAQ+9K0169ARyJ2yoGs+0Z/fHmrXmWNNqOpcuV56A1xX6E+VOpUth8Z8RqPFKnEq4/rFJRKPA9Uhue2KM9yj6hsAbF0ZXU74="
  252.  
  253.   },
  254.  
  255.   "success": false
  256.  
  257.  }
  258.  
  259. */
  260.  
  261.     }
  262.  
  263.  
  264.  
  265.  
  266.     /**
  267.  
  268.      * 自动生成页面授权的url.
  269.  
  270.      * @throws Exception
  271.  
  272.      */
  273.  
  274.     public static void testPageAuth() throws Exception {
  275.  
  276.         ZhimaAuthInfoAuthorizeRequest authInfoAuthorizeRequest = new ZhimaAuthInfoAuthorizeRequest();
  277.  
  278.         authInfoAuthorizeRequest.setChannel("apppc"); // PC端
  279.  
  280.         authInfoAuthorizeRequest.setPlatform("zmop"); // 开放平台
  281.  
  282.         // 0:芝麻信用开放账号ID 1:按照手机号进行授权 2:按照身份证+姓名进行授权 3通过公安网验证进行授权 4.通过人脸验证进行授权
  283.  
  284.         authInfoAuthorizeRequest.setIdentityType("2");
  285.  
  286.         Map<String, String> identityParams = new HashMap<String, String>();
  287.  
  288.         identityParams.put("certNo", "61042619850403354X"); // 证件号码
  289.  
  290.         identityParams.put("name", "张三"); // 姓名
  291.  
  292.         identityParams.put("certType", "IDENTITY_CARD"); // 证件类型
  293.  
  294.         authInfoAuthorizeRequest.setIdentityParam(JSONObject.toJSONString(identityParams));
  295.  
  296.         DefaultZhimaClient client = new DefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
  297.  
  298.         String pageAuthUrl = client.generatePageRedirectInvokeUrl(authInfoAuthorizeRequest);
  299.  
  300.         System.out.println(pageAuthUrl);
  301.  
  302.     }
  303.  
  304.     
  305.  
  306.     /**
  307.  
  308.      * 获取芝麻粉(GET方式)
  309.  
  310.      */
  311.  
  312.     public void testZhimaCreditWatchlistGet() {
  313.  
  314.         ZhimaCreditScoreGetRequest req = new ZhimaCreditScoreGetRequest();
  315.  
  316.         req.setProductCode("w1010100100000000001");// 必要参数        
  317.  
  318.         req.setOpenId("268816231939676969685782895");// 必要参数        
  319.  
  320.         DefaultZhimaClient client = new DefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
  321.  
  322.         try {
  323.  
  324.             // 如果正常返回,直接在对象里面获取结果值
  325.  
  326.             ZhimaCreditScoreGetResponse response = client.execute(req);
  327.  
  328.             System.out.println(JSON.toJSON(response));
  329.  
  330.         } catch (ZhimaApiException e) {
  331.  
  332.             e.printStackTrace();
  333.  
  334.         }
  335.  
  336.     }
  337.  
  338.     
  339.  
  340.     /**
  341.  
  342.      * 处理回调后的参数,然后解密params
  343.  
  344.      * @param url 例如:http://xxxx.comparams%3Dxxxxx% ... xxxxx
  345.  
  346.      * @throws Exception
  347.  
  348.      */
  349.  
  350.     public static void parseFromReturnUrl(String url) throws Exception {
  351.  
  352.         int index = url.indexOf("");
  353.  
  354.         String urlParamString = url.substring(index + 1);
  355.  
  356.         String[] paraPairs = urlParamString.split("&");
  357.  
  358.         String encryptedParam = "";
  359.  
  360.         for (String paramPair : paraPairs) {
  361.  
  362.             String[] splits = paramPair.split("=");
  363.  
  364.             if ("params".equals(splits[0])) {
  365.  
  366.                 encryptedParam = splits[1];
  367.  
  368.             }
  369.  
  370.         }
  371.  
  372.         String decryptedParam = RSACoderUtil.decrypt(URLDecoder.decode(encryptedParam, CHARSET),
  373.  
  374.             PRIKEY, CHARSET);
  375.  
  376.         //通过浏览器返回时,不需要decode
  377.  
  378.         System.out.println(URLDecoder.decode(decryptedParam, CHARSET));
  379.  
  380. /*
  381.  
  382.         params主要JSON参数如下:
  383.  
  384.                  名称                类型          示例值             备注
  385.  
  386.         success          String     success     请求成功还是失败的标识
  387.  
  388.         error_code      String     000001        失败时的错误码
  389.  
  390.         error_message    String    缺少appId    失败时的错误信息
  391.  
  392.         open_id            String    26881...    芝麻业务id
  393.  
  394.         state            String    239...        商户透传的值,芝麻不做解析
  395.  
  396. */
  397.  
  398.     }
  399.  
  400. }
3 0
原创粉丝点击