本文实例为大家分享了java调用用户芝麻信用分的具体代码,供大家参考,具体内容如下
1.导入芝麻信用api: zmxy-sdk-java-20180824112425.jar 和 fastjson-1.2.48.jar
2.代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
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测试数据.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();
}
}
/**
* 获取目标用户的open_id,没有openid,走授权
*/
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));
/* 成功响应报文 {
"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 (zhimaapiexception e) {
e.printstacktrace();
}
}
/**
* 测试查询芝麻分
* @throws zhimaapiexception
*/
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));
/* 成功查询响应报文
{
"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
* @throws exception
*/
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<string, string> identityparams = new hashmap<string, string>();
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);
}
/**
* 获取芝麻分(get方式)
*/
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();
}
}
/**
* 处理回调后的参数,然后解密params
* @param url 例如:http://xxxx测试数据params%3dxxxxx% ... xxxxx
* @throws exception
*/
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));
/* params主要json参数如下:
名称 类型 示例值 备注
success string success 请求成功还是失败的标识
error_code string 000001 失败时的错误码
error_message string 缺少appid 失败时的错误信息
open_id string 26881... 芝麻业务id
state string 239... 商户透传的值,芝麻不做解析
*/
}
} |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/confusing_awakening/article/details/83215980