怎么在Java項(xiàng)目中調(diào)用用戶的芝麻信用分?相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
成都創(chuàng)新互聯(lián) - 德陽(yáng)服務(wù)器托管,四川服務(wù)器租用,成都服務(wù)器租用,四川網(wǎng)通托管,綿陽(yáng)服務(wù)器托管,德陽(yáng)服務(wù)器托管,遂寧服務(wù)器托管,綿陽(yáng)服務(wù)器托管,四川云主機(jī),成都云主機(jī),西南云主機(jī),德陽(yáng)服務(wù)器托管,西南服務(wù)器托管,四川/成都大帶寬,成都機(jī)柜租用,四川老牌IDC服務(wù)商
1.導(dǎo)入芝麻信用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 { //芝麻開(kāi)放平臺(tái)地址 private static final String URL = "https://zmopenapi.zmxy.com.cn/openapi.do"; //商戶應(yīng)用 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"; /** * 查詢授權(quán)信息 */ public void testZhimaAuthInfoAuthorize() { ZhimaAuthInfoAuthorizeRequest req = new ZhimaAuthInfoAuthorizeRequest(); req.setIdentityType("2");// 身份標(biāo)識(shí) req.setChannel("apppc"); // PC端 // 必要參數(shù) state: 用于給商戶提供透?jìng)鞯膮?shù),芝麻會(huì)將此參數(shù)透?jìng)鹘o商戶 req.setBizParams("{\"auth_code\":\"M_APPPC_CERT\",\"state\":\"100111211\"}"); req.setIdentityParam( "{\"certNo\":\"330621198710114617\",\"certType\":\"IDENTITY_CARD\",\"name\":\"陳金賽\"}");// 必要參數(shù) DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY); try { String url = client.generatePageRedirectInvokeUrl(req); System.out.println(url); } catch (ZhimaApiException e) { e.printStackTrace(); } } /** * 獲取目標(biāo)用戶的open_id,沒(méi)有openid,走授權(quán) */ public void testZhimaAuthInfoReq() { ZhimaAuthInfoAuthqueryRequest req = new ZhimaAuthInfoAuthqueryRequest(); // 0:芝麻信用開(kāi)放賬號(hào)ID 1:按照手機(jī)號(hào)進(jìn)行授權(quán) 2:按照身份證+姓名進(jìn)行授權(quán) 3通過(guò)公安網(wǎng)驗(yàn)證進(jìn)行授權(quán) 4.通過(guò)人臉驗(yàn)證進(jìn)行授權(quán) req.setIdentityType("2"); req.setIdentityParam( "{\"certNo\":\"522121198710114617\",\"certType\":\"IDENTITY_CARD\",\"name\":\"但鏡宇\"}");// 必要參數(shù) DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY); try { // 如果正常返回,直接在對(duì)象里面獲取結(jié)果值 ZhimaAuthInfoAuthqueryResponse response = client.execute(req); System.out.println(JSON.toJSON(response)); /* 成功響應(yīng)報(bào)文 { "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 } // 失敗響應(yīng)報(bào)文 { "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(); } } /** * 測(cè)試查詢芝麻分 * @throws ZhimaApiException */ public static void testQueryScore() throws ZhimaApiException { ZhimaCreditScoreGetRequest creditScoreGetRequest = new ZhimaCreditScoreGetRequest(); creditScoreGetRequest.setPlatform("zmop"); // 開(kāi)放平臺(tái),zmop代表芝麻開(kāi)放平臺(tái) creditScoreGetRequest.setChannel("apppc"); // pc端 //transactionId,該標(biāo)記是商戶每次請(qǐng)求的唯一標(biāo)識(shí)。建議使用uuid進(jìn)行傳遞, creditScoreGetRequest.setTransactionId(UUID.randomUUID().toString()); creditScoreGetRequest.setProductCode("w1010100100000000001"); // 商戶配置那塊兒的產(chǎn)品Code creditScoreGetRequest.setOpenId("268816113399909561399995894"); // appid,每個(gè)人的標(biāo)識(shí) DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY); // 如果正常返回,直接在對(duì)象里面獲取結(jié)果值 ZhimaCreditScoreGetResponse creditScoreGetResponse = client.execute(creditScoreGetRequest); System.out.println(JSON.toJSON(creditScoreGetResponse)); /* 成功查詢響應(yīng)報(bào)文 { "bizNo": "ZM20160120014501763890a37b16614532255017521309", "body": "{\"success\":true,\"biz_no\":\"ZM20160120014501763890a37b16614532255017521309\",\"zm_score\":751}", "params": { "params": "aqiMCUg1VbZRqflPyVyLKgxShcvc3WoWLTFewqA9PvQjp6yuC99lYlGdr/Bf2FevQ2zemdPTixqQWnO7qdz0pT2x4VFWVJdldPgLVsRr37qWDcO5NjvNZzOuEYozzGSnzwhTyelqglAdzOOepM0ows+7sNVJriABtRdt1HH8gqc=" }, "success": true, "zmScore": "751" } // 失敗查詢響應(yīng)報(bào)文 { "body": "{\"success\":false,\"error_code\":\"ZMCREDIT.api_product_not_match\",\"error_message\":\"輸入的產(chǎn)品碼不正確\"}", "errorCode": "ZMCREDIT.api_product_not_match", "errorMessage": "輸入的產(chǎn)品碼不正確", "params": { "params": "vJTcbtVvo0/WEG01Flve0//EbE70clk7v84B7MVJZH59/HefdyAZ1JG4opTna5ANWvY8UArsGlVIAQ+9K0169ARyJ2yoGs+0Z/fHmrXmWNNqOpcuV56A1xX6E+VOpUth8Z8RqPFKnEq4/rFJRKPA9Uhue2KM9yj6hsAbF0ZXU74=" }, "success": false } */ } /** * 自動(dòng)生成頁(yè)面授權(quán)的url * @throws Exception */ public static void testPageAuth() throws Exception { ZhimaAuthInfoAuthorizeRequest authInfoAuthorizeRequest = new ZhimaAuthInfoAuthorizeRequest(); authInfoAuthorizeRequest.setChannel("apppc"); // PC端 authInfoAuthorizeRequest.setPlatform("zmop"); // 開(kāi)放平臺(tái) // 0:芝麻信用開(kāi)放賬號(hào)ID 1:按照手機(jī)號(hào)進(jìn)行授權(quán) 2:按照身份證+姓名進(jìn)行授權(quán) 3通過(guò)公安網(wǎng)驗(yàn)證進(jìn)行授權(quán) 4.通過(guò)人臉驗(yàn)證進(jìn)行授權(quán) authInfoAuthorizeRequest.setIdentityType("2"); Map<String, String> identityParams = new HashMap<String, String>(); identityParams.put("certNo", "61042619850403354X"); // 證件號(hào)碼 identityParams.put("name", "張三"); // 姓名 identityParams.put("certType", "IDENTITY_CARD"); // 證件類(lèi)型 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");// 必要參數(shù) req.setOpenId("268816231939676969685782895");// 必要參數(shù) DefaultZhimaClient client = new DefaultZhimaClient(URL, APPID, CHARSET, PRIKEY, PUBKEY); try { // 如果正常返回,直接在對(duì)象里面獲取結(jié)果值 ZhimaCreditScoreGetResponse response = client.execute(req); System.out.println(JSON.toJSON(response)); } catch (ZhimaApiException e) { e.printStackTrace(); } } /** * 處理回調(diào)后的參數(shù),然后解密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); //通過(guò)瀏覽器返回時(shí),不需要decode System.out.println(URLDecoder.decode(decryptedParam, CHARSET)); /* params主要JSON參數(shù)如下: 名稱 類(lèi)型 示例值 備注 success String success 請(qǐng)求成功還是失敗的標(biāo)識(shí) error_code String 000001 失敗時(shí)的錯(cuò)誤碼 error_message String 缺少appId 失敗時(shí)的錯(cuò)誤信息 open_id String 26881... 芝麻業(yè)務(wù)id state String 239... 商戶透?jìng)鞯闹?,芝麻不做解? */ } }
看完上述內(nèi)容,你們掌握怎么在Java項(xiàng)目中調(diào)用用戶的芝麻信用分的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
網(wǎng)頁(yè)題目:怎么在Java項(xiàng)目中調(diào)用用戶的芝麻信用分
文章分享:http://m.newbst.com/article12/pgcidc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、用戶體驗(yàn)、云服務(wù)器、全網(wǎng)營(yíng)銷(xiāo)推廣、軟件開(kāi)發(fā)、建站公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)