本文实例为大家分享了Java调用用户芝麻信用分的具体代码,供大家参考,具体内容如下
1.导入芝麻信用API:zmxy-sdk-java-20180824112425.jar 和 fastjson-1.2.48.jar
2.代码如下:
package com.zhima;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
import com.antgroup.zmxy.openplatform.api.ZhimaApiException;
import com.antgroup.zmxy.openplatform.api.internal.util.RSACoderUtil;
import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthorizeRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthqueryRequest;
import com.antgroup.zmxy.openplatform.api.request.ZhimaCreditScoreGetRequest;
import com.antgroup.zmxy.openplatform.api.response.ZhimaAuthInfoAuthqueryResponse;
import com.antgroup.zmxy.openplatform.api.response.ZhimaCreditScoreGetResponse;
public class Demo {
//芝麻开放平台地址
private static final String URL = "https://zmopenapi.zmxy.com.cn/openapi.do";
//商户应用 Id
private static final String APPID = "";
//商户 RSA 私钥
private static final String PRIKEY = "";
//芝麻 RSA 公钥
private static final String PUBKEY = "";
//统一字符集
private static final String CHARSET = "UTF-8";
public void testZhimaAuthInfoAuthorize() {
ZhimaAuthInfoAuthorizeRequest req = new ZhimaAuthInfoAuthorizeRequest();
req.setIdentityType("2");// 身份标识
req.setChannel("apppc"); // PC端
// 必要参数 state: 用于给商户提供透传的参数,芝麻会将此参数透传给商户
req.setBizParams("{"auth_code":"M_APPPC_CERT","state":"100111211"}");
req.setIdentityParam(
"{"certNo":"330621198710114617","certType":"IDENTITY_CARD","name":"陈金赛"}");// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY);
try {
String url = client.generatePageRedirectInvokeUrl(req);
System.out.println(url);
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
public void testZhimaAuthInfoReq() {
ZhimaAuthInfoAuthqueryRequest req = new ZhimaAuthInfoAuthqueryRequest();
// 0:芝麻信用开放账号ID 1:按照手机号进行授权 2:按照身份证+姓名进行授权 3通过公安网验证进行授权 4.通过人脸验证进行授权
req.setIdentityType("2");
req.setIdentityParam(
"{"certNo":"522121198710114617","certType":"IDENTITY_CARD","name":"但镜宇"}");// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY);
try {
// 如果正常返回,直接在对象里面获取结果值
ZhimaAuthInfoAuthqueryResponse response = client.execute(req);
System.out.println(JSON.toJSON(response));
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
public static void testQueryScore() throws ZhimaApiException {
ZhimaCreditScoreGetRequest creditScoreGetRequest = new ZhimaCreditScoreGetRequest();
creditScoreGetRequest.setPlatform("zmop"); // 开放平台,zmop代表芝麻开放平台
creditScoreGetRequest.setChannel("apppc"); // pc端
//transactionId,该标记是商户每次请求的唯一标识。建议使用uuid进行传递,
creditScoreGetRequest.setTransactionId(UUID.randomUUID().toString());
creditScoreGetRequest.setProductCode("w1010100100000000001"); // 商户配置那块儿的产品Code
creditScoreGetRequest.setOpenId("268816113399909561399995894"); // appid,每个人的标识
DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY);
// 如果正常返回,直接在对象里面获取结果值
ZhimaCreditScoreGetResponse creditScoreGetResponse = client.execute(creditScoreGetRequest);
System.out.println(JSON.toJSON(creditScoreGetResponse));
}
public static void testPageAuth() throws Exception {
ZhimaAuthInfoAuthorizeRequest authInfoAuthorizeRequest = new ZhimaAuthInfoAuthorizeRequest();
authInfoAuthorizeRequest.setChannel("apppc"); // PC端
authInfoAuthorizeRequest.setPlatform("zmop"); // 开放平台
// 0:芝麻信用开放账号ID 1:按照手机号进行授权 2:按照身份证+姓名进行授权 3通过公安网验证进行授权 4.通过人脸验证进行授权
authInfoAuthorizeRequest.setIdentityType("2");
Map identityParams = new HashMap();
identityParams.put("certNo", "61042619850403354X"); // 证件号码
identityParams.put("name", "张三"); // 姓名
identityParams.put("certType", "IDENTITY_CARD"); // 证件类型
authInfoAuthorizeRequest.setIdentityParam(JSONObject.toJSonString(identityParams));
DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY);
String pageAuthUrl = client.generatePageRedirectInvokeUrl(authInfoAuthorizeRequest);
System.out.println(pageAuthUrl);
}
public void testZhimaCreditWatchlistGet() {
ZhimaCreditScoreGetRequest req = new ZhimaCreditScoreGetRequest();
req.setProductCode("w1010100100000000001");// 必要参数
req.setOpenId("268816231939676969685782895");// 必要参数
DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY);
try {
// 如果正常返回,直接在对象里面获取结果值
ZhimaCreditScoreGetResponse response = client.execute(req);
System.out.println(JSON.toJSON(response));
} catch (ZhimaApiException e) {
e.printStackTrace();
}
}
public static void parseFromReturnUrl(String url) throws Exception {
int index = url.indexOf("");
String urlParamString = url.substring(index + 1);
String[] paraPairs = urlParamString.split("&");
String encryptedParam = "";
for (String paramPair : paraPairs) {
String[] splits = paramPair.split("=");
if ("params".equals(splits[0])) {
encryptedParam = splits[1];
}
}
String decryptedParam = RSACoderUtil.decrypt(URLDecoder.decode(encryptedParam, CHARSET),
PRIKEY, CHARSET);
//通过浏览器返回时,不需要decode
System.out.println(URLDecoder.decode(decryptedParam, CHARSET));
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



