Java调用用户芝麻信用分
本文实例为大家分享了Java调用用户芝麻信用分的具体代码,供大家参考,具体内容如下
1.导入芝麻信用API:zmxy-sdk-java-20180824112425.jar
2.代码如下:
packagecom.zhima;
importjava.net.URLDecoder;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.UUID;
importcom.alibaba.fastjson.JSON;
importcom.alibaba.fastjson.JSONObject;
importcom.antgroup.zmxy.openplatform.api.DefaultZhimaClient;
importcom.antgroup.zmxy.openplatform.api.ZhimaApiException;
importcom.antgroup.zmxy.openplatform.api.internal.util.RSACoderUtil;
importcom.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthorizeRequest;
importcom.antgroup.zmxy.openplatform.api.request.ZhimaAuthInfoAuthqueryRequest;
importcom.antgroup.zmxy.openplatform.api.request.ZhimaCreditScoreGetRequest;
importcom.antgroup.zmxy.openplatform.api.response.ZhimaAuthInfoAuthqueryResponse;
importcom.antgroup.zmxy.openplatform.api.response.ZhimaCreditScoreGetResponse;
publicclassDemo{
//芝麻开放平台地址
privatestaticfinalStringURL="https://zmopenapi.zmxy.com.cn/openapi.do";
//商户应用Id
privatestaticfinalStringAPPID="";
//商户RSA私钥
privatestaticfinalStringPRIKEY="";
//芝麻RSA公钥
privatestaticfinalStringPUBKEY="";
//统一字符集
privatestaticfinalStringCHARSET="UTF-8";
/**
*查询授权信息
*/
publicvoidtestZhimaAuthInfoAuthorize(){
ZhimaAuthInfoAuthorizeRequestreq=newZhimaAuthInfoAuthorizeRequest();
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\":\"陈金赛\"}");//必要参数
DefaultZhimaClientclient=newDefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
try{
Stringurl=client.generatePageRedirectInvokeUrl(req);
System.out.println(url);
}catch(ZhimaApiExceptione){
e.printStackTrace();
}
}
/**
*获取目标用户的open_id,没有openid,走授权
*/
publicvoidtestZhimaAuthInfoReq(){
ZhimaAuthInfoAuthqueryRequestreq=newZhimaAuthInfoAuthqueryRequest();
//0:芝麻信用开放账号ID1:按照手机号进行授权2:按照身份证+姓名进行授权3通过公安网验证进行授权4.通过人脸验证进行授权
req.setIdentityType("2");
req.setIdentityParam(
"{\"certNo\":\"522121198710114617\",\"certType\":\"IDENTITY_CARD\",\"name\":\"但镜宇\"}");//必要参数
DefaultZhimaClientclient=newDefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
try{
//如果正常返回,直接在对象里面获取结果值
ZhimaAuthInfoAuthqueryResponseresponse=client.execute(req);
System.out.println(JSON.toJSON(response));
/*成功响应报文
{
"authorized":true,
"body":"{\"success\":true,\"authorized\":true,\"open_id\":\"268816231939676969685782895\"}",
"openId":"268816231939676969685782895",
"params":{
"params":"faeSa69w3kOL+ZRAuHykvufu6ogiqxA48l5hzPutKItPy/2HKtdznbzvLhutsY6KTqLgwS/Af1GN1Y7MLh+XDFtjVUf5kF/npUspRkyFUCPpASJAMxeK56vaELnZ4yCVFjVseOBYlCn4YOCZWmBpu02l7IctE6ZP3r1o4HUR+sMQYlYu2xFHfXiqX2g0vdQn81pJ/fcQ4OuBzq1d3c+qniBXf3iESni1m21nMjxeUclzCdZwXWCI1aIYQnDFgVo/58kJ9CTw/6FrffUcP+c6zvzdM8RNCefiGGuF5Vef+C6tck1kBaYoYT6BS5u4ahYteoiG+XJgzLKF+BqAplhq/A=="
},
"success":true
}
//失败响应报文
{
"authorized":false,
"body":"{\"success\":true,\"authorized\":false}",
"params":{
"params":"lGYId9CaU8McGDd8U0ZMM8Pky1/ulOE44LkgGcrWrdzmEtU1JBT5M+Jjd5e27bz45d/CNEqbwmwtg3VsozZTol42YKNi+MYzvvyZEOsUT9F68qiIPxxlOv3vGJsUpB+LJMOI8ZU7eXtHoCnbarQSOvtSylilp6dpIbAIYxbPPvSkj+7f1TiSne/7Re/XZ1qVVM18yTJyleCr+WlOe7o59lZAoiZ5bDoP2ta8MIjwlxz4/1d+IYe0f2BoZQa/9oDqJZ0MNQ0iPCbhqBKRuShPCQe8afEyOTTI0p+cROjwWTz0dwP7QXnkWBvzX3hrWXImdk7JszOVoSxAAAfwcVXNxw=="
},
"success":true
}*/
}catch(ZhimaApiExceptione){
e.printStackTrace();
}
}
/**
*测试查询芝麻分
*@throwsZhimaApiException
*/
publicstaticvoidtestQueryScore()throwsZhimaApiException{
ZhimaCreditScoreGetRequestcreditScoreGetRequest=newZhimaCreditScoreGetRequest();
creditScoreGetRequest.setPlatform("zmop");//开放平台,zmop代表芝麻开放平台
creditScoreGetRequest.setChannel("apppc");//pc端
//transactionId,该标记是商户每次请求的唯一标识。建议使用uuid进行传递,
creditScoreGetRequest.setTransactionId(UUID.randomUUID().toString());
creditScoreGetRequest.setProductCode("w1010100100000000001");//商户配置那块儿的产品Code
creditScoreGetRequest.setOpenId("268816113399909561399995894");//appid,每个人的标识
DefaultZhimaClientclient=newDefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
//如果正常返回,直接在对象里面获取结果值
ZhimaCreditScoreGetResponsecreditScoreGetResponse=client.execute(creditScoreGetRequest);
System.out.println(JSON.toJSON(creditScoreGetResponse));
/*成功查询响应报文
{
"bizNo":"ZM20160120014501763890a37b16614532255017521309",
"body":"{\"success\":true,\"biz_no\":\"ZM20160120014501763890a37b16614532255017521309\",\"zm_score\":751}",
"params":{
"params":"aqiMCUg1VbZRqflPyVyLKgxShcvc3WoWLTFewqA9PvQjp6yuC99lYlGdr/Bf2FevQ2zemdPTixqQWnO7qdz0pT2x4VFWVJdldPgLVsRr37qWDcO5NjvNZzOuEYozzGSnzwhTyelqglAdzOOepM0ows+7sNVJriABtRdt1HH8gqc="
},
"success":true,
"zmScore":"751"
}
//失败查询响应报文
{
"body":"{\"success\":false,\"error_code\":\"ZMCREDIT.api_product_not_match\",\"error_message\":\"输入的产品码不正确\"}",
"errorCode":"ZMCREDIT.api_product_not_match",
"errorMessage":"输入的产品码不正确",
"params":{
"params":"vJTcbtVvo0/WEG01Flve0//EbE70clk7v84B7MVJZH59/HefdyAZ1JG4opTna5ANWvY8UArsGlVIAQ+9K0169ARyJ2yoGs+0Z/fHmrXmWNNqOpcuV56A1xX6E+VOpUth8Z8RqPFKnEq4/rFJRKPA9Uhue2KM9yj6hsAbF0ZXU74="
},
"success":false
}*/
}
/**
*自动生成页面授权的url
*@throwsException
*/
publicstaticvoidtestPageAuth()throwsException{
ZhimaAuthInfoAuthorizeRequestauthInfoAuthorizeRequest=newZhimaAuthInfoAuthorizeRequest();
authInfoAuthorizeRequest.setChannel("apppc");//PC端
authInfoAuthorizeRequest.setPlatform("zmop");//开放平台
//0:芝麻信用开放账号ID1:按照手机号进行授权2:按照身份证+姓名进行授权3通过公安网验证进行授权4.通过人脸验证进行授权
authInfoAuthorizeRequest.setIdentityType("2");
MapidentityParams=newHashMap();
identityParams.put("certNo","61042619850403354X");//证件号码
identityParams.put("name","张三");//姓名
identityParams.put("certType","IDENTITY_CARD");//证件类型
authInfoAuthorizeRequest.setIdentityParam(JSONObject.toJSONString(identityParams));
DefaultZhimaClientclient=newDefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
StringpageAuthUrl=client.generatePageRedirectInvokeUrl(authInfoAuthorizeRequest);
System.out.println(pageAuthUrl);
}
/**
*获取芝麻分(GET方式)
*/
publicvoidtestZhimaCreditWatchlistGet(){
ZhimaCreditScoreGetRequestreq=newZhimaCreditScoreGetRequest();
req.setProductCode("w1010100100000000001");//必要参数
req.setOpenId("268816231939676969685782895");//必要参数
DefaultZhimaClientclient=newDefaultZhimaClient(URL,APPID,CHARSET,PRIKEY,PUBKEY);
try{
//如果正常返回,直接在对象里面获取结果值
ZhimaCreditScoreGetResponseresponse=client.execute(req);
System.out.println(JSON.toJSON(response));
}catch(ZhimaApiExceptione){
e.printStackTrace();
}
}
/**
*处理回调后的参数,然后解密params
*@paramurl例如:http://xxxx.comparams%3Dxxxxx%...xxxxx
*@throwsException
*/
publicstaticvoidparseFromReturnUrl(Stringurl)throwsException{
intindex=url.indexOf("");
StringurlParamString=url.substring(index+1);
String[]paraPairs=urlParamString.split("&");
StringencryptedParam="";
for(StringparamPair:paraPairs){
String[]splits=paramPair.split("=");
if("params".equals(splits[0])){
encryptedParam=splits[1];
}
}
StringdecryptedParam=RSACoderUtil.decrypt(URLDecoder.decode(encryptedParam,CHARSET),
PRIKEY,CHARSET);
//通过浏览器返回时,不需要decode
System.out.println(URLDecoder.decode(decryptedParam,CHARSET));
/*params主要JSON参数如下:
名称类型示例值备注
successStringsuccess请求成功还是失败的标识
error_codeString000001失败时的错误码
error_messageString缺少appId失败时的错误信息
open_idString26881...芝麻业务id
stateString239...商户透传的值,芝麻不做解析
*/
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。