您好,登錄后才能下訂單哦!
怎么在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 = ""; //統(tǒng)一字符集 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... 芝麻業(yè)務id state String 239... 商戶透傳的值,芝麻不做解析 */ } }
看完上述內容,你們掌握怎么在Java項目中調用用戶的芝麻信用分的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。