欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java调用用户芝麻信用分

程序员文章站 2024-03-05 12:37:48
本文实例为大家分享了java调用用户芝麻信用分的具体代码,供大家参考,具体内容如下 1.导入芝麻信用api:zmxy-sdk-java-20180824112425.ja...

本文实例为大家分享了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();

  }

 }

 /**
 
  * 获取目标用户的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.comparams%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...  商户透传的值,芝麻不做解析
   
  */

 }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。