|
|
@@ -442,7 +442,7 @@ public class TaobaoServiceImpl implements TaobaoService {
|
|
442
|
442
|
|
|
443
|
443
|
@Override
|
|
444
|
444
|
public void getTaobaoAuthorizedLoginToken(String code, String state) throws IOException {
|
|
445
|
|
- Map<String, String> params = new HashMap<String, String>();
|
|
|
445
|
+ Map<String, String> params = new HashMap<>();
|
|
446
|
446
|
// 公共参数
|
|
447
|
447
|
params.put("method", "taobao.top.auth.token.create");
|
|
448
|
448
|
params.put("app_key", tbAppKey);
|
|
|
@@ -453,24 +453,92 @@ public class TaobaoServiceImpl implements TaobaoService {
|
|
453
|
453
|
params.put("sign_method", "hmac");
|
|
454
|
454
|
params.put("code", code);
|
|
455
|
455
|
params.put("sign", SignTop.signTopRequest(params, tbkAppSecret, "hmac"));
|
|
|
456
|
+
|
|
456
|
457
|
System.out.println("***************taobao.top.auth.token.create start *****************");
|
|
457
|
|
- System.out.println(params.toString());
|
|
458
|
|
- String a = SignTop.callApi(new URL("https://eco.taobao.com/router/rest"), params);
|
|
459
|
|
- System.out.println("原始响应字符串: " + a);
|
|
460
|
|
- JSONObject jsonObjects = JSONObject.parseObject(a);
|
|
461
|
|
- System.out.println("解析为JSONObject后: " + jsonObjects.toJSONString());
|
|
462
|
|
- System.out.println("***************taobao.top.auth.token.create* end****************");
|
|
463
|
|
- Object token = JSONObject.parseObject(JSONObject.parseObject(jsonObjects.get("top_auth_token_create_response").toString()).get("token_result").toString()).get("access_token");
|
|
464
|
|
- //备案
|
|
465
|
|
- demo(token.toString(),state);
|
|
|
458
|
+ System.out.println(params);
|
|
|
459
|
+ String responseString;
|
|
|
460
|
+ try {
|
|
|
461
|
+ responseString = SignTop.callApi(new URL("https://eco.taobao.com/router/rest"), params);
|
|
|
462
|
+ } catch (IOException e) {
|
|
|
463
|
+ throw new RuntimeException("淘宝授权网络请求失败", e);
|
|
|
464
|
+ }
|
|
|
465
|
+ System.out.println("原始响应字符串: " + responseString);
|
|
|
466
|
+ if (responseString == null || responseString.trim().isEmpty()) {
|
|
|
467
|
+ throw new RuntimeException("淘宝授权响应为空");
|
|
|
468
|
+ }
|
|
|
469
|
+
|
|
|
470
|
+ JSONObject jsonObject;
|
|
|
471
|
+ try {
|
|
|
472
|
+ jsonObject = JSONObject.parseObject(responseString);
|
|
|
473
|
+ } catch (Exception e) {
|
|
|
474
|
+ throw new RuntimeException("淘宝授权响应JSON解析失败", e);
|
|
|
475
|
+ }
|
|
|
476
|
+ System.out.println("解析为JSONObject后: " + jsonObject.toJSONString());
|
|
|
477
|
+ System.out.println("*************** taobao.top.auth.token.create end *****************");
|
|
|
478
|
+
|
|
|
479
|
+ // 1. 检查淘宝业务错误
|
|
|
480
|
+ if (jsonObject.containsKey("error_response")) {
|
|
|
481
|
+ JSONObject error = jsonObject.getJSONObject("error_response");
|
|
|
482
|
+ String code_ = error.getString("code");
|
|
|
483
|
+ String msg = error.getString("msg");
|
|
|
484
|
+ String subMsg = error.getString("sub_msg");
|
|
|
485
|
+ throw new RuntimeException(String.format("淘宝授权业务错误 [code=%s, msg=%s, subMsg=%s]", code_, msg, subMsg));
|
|
|
486
|
+ }
|
|
|
487
|
+ // 2. 获取 top_auth_token_create_response
|
|
|
488
|
+ JSONObject createResp = jsonObject.getJSONObject("top_auth_token_create_response");
|
|
|
489
|
+ if (createResp == null) {
|
|
|
490
|
+ throw new RuntimeException("响应缺少 top_auth_token_create_response 字段");
|
|
|
491
|
+ }
|
|
|
492
|
+ // 3. 获取 token_result 字符串
|
|
|
493
|
+ String tokenResultStr = createResp.getString("token_result");
|
|
|
494
|
+ if (tokenResultStr == null || tokenResultStr.isEmpty()) {
|
|
|
495
|
+ throw new RuntimeException("token_result 字段为空");
|
|
|
496
|
+ }
|
|
|
497
|
+ // 4. 解析 token_result
|
|
|
498
|
+ JSONObject tokenResult;
|
|
|
499
|
+ try {
|
|
|
500
|
+ tokenResult = JSONObject.parseObject(tokenResultStr);
|
|
|
501
|
+ } catch (Exception e) {
|
|
|
502
|
+ throw new RuntimeException("token_result JSON解析失败: " + tokenResultStr, e);
|
|
|
503
|
+ }
|
|
|
504
|
+ // 5. 获取 access_token
|
|
|
505
|
+ String accessToken = tokenResult.getString("access_token");
|
|
|
506
|
+ if (accessToken == null || accessToken.isEmpty()) {
|
|
|
507
|
+ throw new RuntimeException("access_token 字段为空");
|
|
|
508
|
+ }
|
|
|
509
|
+
|
|
|
510
|
+ // 6. 备案
|
|
|
511
|
+ demo(accessToken, state);
|
|
466
|
512
|
}
|
|
467
|
513
|
//备案
|
|
468
|
514
|
private void demo(String token,String state) throws IOException {
|
|
|
515
|
+// if (state == null || state.trim().isEmpty()) {
|
|
|
516
|
+// throw new RuntimeException("state 参数为空");
|
|
|
517
|
+// }
|
|
|
518
|
+//
|
|
|
519
|
+// String cleanState = state.trim();
|
|
|
520
|
+// // 移除 UTF-8 BOM(如果存在)
|
|
|
521
|
+// if (cleanState.startsWith("\uFEFF")) {
|
|
|
522
|
+// cleanState = cleanState.substring(1);
|
|
|
523
|
+// }
|
|
|
524
|
+
|
|
|
525
|
+ JSONObject stateObject;
|
|
|
526
|
+ try {
|
|
|
527
|
+ stateObject = JSONObject.parseObject(state);
|
|
|
528
|
+ } catch (Exception e) {
|
|
|
529
|
+ System.err.println("state 原始字符串长度: " + state.length());
|
|
|
530
|
+ System.err.println("state 前100字符: " + state.substring(0, Math.min(100, state.length())));
|
|
|
531
|
+ System.err.println("state 后100字符: " + state.substring(Math.max(0, state.length() - 100)));
|
|
|
532
|
+ // 可选:打印十六进制字节,用于排查编码问题
|
|
|
533
|
+ System.err.println("state 字节数组(hex): " + bytesToHex(state.getBytes(StandardCharsets.UTF_8)));
|
|
|
534
|
+ throw new RuntimeException("state 参数JSON解析失败: " + state, e);
|
|
|
535
|
+ }
|
|
|
536
|
+ String uid = stateObject.getString("uid");
|
|
|
537
|
+ if (uid == null) {
|
|
|
538
|
+ throw new RuntimeException("state 中缺少 uid 字段");
|
|
|
539
|
+ }
|
|
469
|
540
|
|
|
470
|
|
- JSONObject stateObject = JSONObject.parseObject(state);
|
|
471
|
|
-
|
|
472
|
|
- Map<String, String> params = new HashMap<String, String>();
|
|
473
|
|
- // 公共参数
|
|
|
541
|
+ Map<String, String> params = new HashMap<>();
|
|
474
|
542
|
params.put("method", "taobao.tbk.sc.publisher.info.save");
|
|
475
|
543
|
params.put("app_key", tbAppKey);
|
|
476
|
544
|
params.put("session", token);
|
|
|
@@ -478,39 +546,95 @@ public class TaobaoServiceImpl implements TaobaoService {
|
|
478
|
546
|
params.put("timestamp", df.format(new Date()));
|
|
479
|
547
|
params.put("format", "json");
|
|
480
|
548
|
params.put("v", "2.0");
|
|
481
|
|
- params.put("sign_method","hmac");
|
|
|
549
|
+ params.put("sign_method", "hmac");
|
|
482
|
550
|
// 业务参数
|
|
483
|
|
- // 会员
|
|
484
|
|
- // params.put("inviter_code","RQHQ8Z");
|
|
485
|
|
- // 渠道
|
|
486
|
|
- params.put("inviter_code","BCS4V5");
|
|
487
|
|
- params.put("info_type","1");
|
|
488
|
|
- params.put("note","幸福_"+stateObject.get("uid"));
|
|
489
|
|
- // 签名参数
|
|
|
551
|
+ params.put("inviter_code", "BCS4V5"); // 渠道邀请码
|
|
|
552
|
+ params.put("info_type", "1");
|
|
|
553
|
+ params.put("note", "幸福_" + uid);
|
|
490
|
554
|
params.put("sign", SignTop.signTopRequest(params, tbkAppSecret, "hmac"));
|
|
491
|
|
- // 请用API
|
|
492
|
|
- System.out.println("***************taobao.tbk.sc.publisher.info.save start *****************");
|
|
493
|
|
- System.out.println(params.toString());
|
|
494
|
|
- String callApi = SignTop.callApi(new URL("https://eco.taobao.com/router/rest"), params);
|
|
495
|
|
- // 打印原始字符串
|
|
496
|
|
- System.out.println("原始响应字符串: " + callApi);
|
|
497
|
|
- JSONObject jsonObjects = JSONObject.parseObject(callApi);
|
|
498
|
|
- // 打印解析后的JSONObject(使用toString,但通常toString没有格式化,我们可以用toJSONString来美化)
|
|
499
|
|
- System.out.println("解析为JSONObject后: " + jsonObjects.toJSONString());
|
|
500
|
|
- System.out.println("***************taobao.tbk.sc.publisher.info.save end *****************");
|
|
501
|
|
- String data = JSONObject.parseObject(jsonObjects.get("tbk_sc_publisher_info_save_response").toString()).get("data").toString();
|
|
502
|
|
- Object account_name = JSONObject.parseObject(data).get("account_name");
|
|
503
|
|
- Object relation_id = JSONObject.parseObject(data).get("relation_id");
|
|
|
555
|
+
|
|
|
556
|
+ System.out.println("*************** taobao.tbk.sc.publisher.info.save start *****************");
|
|
|
557
|
+ System.out.println(params);
|
|
|
558
|
+
|
|
|
559
|
+ String responseString;
|
|
|
560
|
+ try {
|
|
|
561
|
+ responseString = SignTop.callApi(new URL("https://eco.taobao.com/router/rest"), params);
|
|
|
562
|
+ } catch (IOException e) {
|
|
|
563
|
+ throw new RuntimeException("淘宝备案网络请求失败", e);
|
|
|
564
|
+ }
|
|
|
565
|
+ System.out.println("原始响应字符串: " + responseString);
|
|
|
566
|
+
|
|
|
567
|
+ if (responseString == null || responseString.trim().isEmpty()) {
|
|
|
568
|
+ throw new RuntimeException("淘宝备案响应为空");
|
|
|
569
|
+ }
|
|
|
570
|
+
|
|
|
571
|
+ JSONObject jsonObject;
|
|
|
572
|
+ try {
|
|
|
573
|
+ jsonObject = JSONObject.parseObject(responseString);
|
|
|
574
|
+ } catch (Exception e) {
|
|
|
575
|
+ throw new RuntimeException("淘宝备案响应JSON解析失败: " + responseString, e);
|
|
|
576
|
+ }
|
|
|
577
|
+ System.out.println("解析为JSONObject后: " + jsonObject.toJSONString());
|
|
|
578
|
+ System.out.println("*************** taobao.tbk.sc.publisher.info.save end *****************");
|
|
|
579
|
+
|
|
|
580
|
+ // 1. 检查淘宝业务错误
|
|
|
581
|
+ if (jsonObject.containsKey("error_response")) {
|
|
|
582
|
+ JSONObject error = jsonObject.getJSONObject("error_response");
|
|
|
583
|
+ String code = error.getString("code");
|
|
|
584
|
+ String msg = error.getString("msg");
|
|
|
585
|
+ String subMsg = error.getString("sub_msg");
|
|
|
586
|
+ throw new RuntimeException(String.format("淘宝备案业务错误 [code=%s, msg=%s, subMsg=%s]", code, msg, subMsg));
|
|
|
587
|
+ }
|
|
|
588
|
+
|
|
|
589
|
+ // 2. 获取 tbk_sc_publisher_info_save_response
|
|
|
590
|
+ JSONObject saveResp = jsonObject.getJSONObject("tbk_sc_publisher_info_save_response");
|
|
|
591
|
+ if (saveResp == null) {
|
|
|
592
|
+ throw new RuntimeException("响应缺少 tbk_sc_publisher_info_save_response 字段");
|
|
|
593
|
+ }
|
|
|
594
|
+
|
|
|
595
|
+ // 3. 获取 data 字段(可能是 JSONObject 或 JSONArray?根据文档通常是对象)
|
|
|
596
|
+ Object dataObj = saveResp.get("data");
|
|
|
597
|
+ if (dataObj == null) {
|
|
|
598
|
+ throw new RuntimeException("data 字段为空");
|
|
|
599
|
+ }
|
|
|
600
|
+
|
|
|
601
|
+ JSONObject dataJson;
|
|
|
602
|
+ if (dataObj instanceof JSONObject) {
|
|
|
603
|
+ dataJson = (JSONObject) dataObj;
|
|
|
604
|
+ } else {
|
|
|
605
|
+ try {
|
|
|
606
|
+ dataJson = JSONObject.parseObject(dataObj.toString());
|
|
|
607
|
+ } catch (Exception e) {
|
|
|
608
|
+ throw new RuntimeException("data 字段无法转换为JSONObject: " + dataObj, e);
|
|
|
609
|
+ }
|
|
|
610
|
+ }
|
|
|
611
|
+
|
|
|
612
|
+ String accountName = dataJson.getString("account_name");
|
|
|
613
|
+ String relationId = dataJson.getString("relation_id");
|
|
|
614
|
+ if (accountName == null || relationId == null) {
|
|
|
615
|
+ throw new RuntimeException("备案返回数据中缺少 account_name 或 relation_id");
|
|
|
616
|
+ }
|
|
|
617
|
+
|
|
|
618
|
+ // 4. 保存到数据库
|
|
504
|
619
|
UserThird userThird = new UserThird();
|
|
505
|
|
- userThird.setUid((String) stateObject.get("uid"));
|
|
506
|
|
- userThird.setPid(relation_id.toString());
|
|
507
|
|
- userThird.setPid_name(account_name.toString());
|
|
508
|
|
- userThird.setCreate_time(new Date().getTime()+"");
|
|
|
620
|
+ userThird.setUid(uid);
|
|
|
621
|
+ userThird.setPid(relationId);
|
|
|
622
|
+ userThird.setPid_name(accountName);
|
|
|
623
|
+ userThird.setCreate_time(String.valueOf(System.currentTimeMillis()));
|
|
509
|
624
|
userThird.setType("5");
|
|
510
|
625
|
userThirdDao.inster(userThird);
|
|
511
|
|
- System.out.println(jsonObjects);
|
|
512
|
|
- //return s;
|
|
513
|
626
|
|
|
|
627
|
+ System.out.println("备案成功,uid=" + uid + ", relation_id=" + relationId);
|
|
|
628
|
+
|
|
|
629
|
+ }
|
|
|
630
|
+
|
|
|
631
|
+ // 辅助方法:字节数组转十六进制
|
|
|
632
|
+ private static String bytesToHex(byte[] bytes) {
|
|
|
633
|
+ StringBuilder sb = new StringBuilder();
|
|
|
634
|
+ for (byte b : bytes) {
|
|
|
635
|
+ sb.append(String.format("%02x", b));
|
|
|
636
|
+ }
|
|
|
637
|
+ return sb.toString();
|
|
514
|
638
|
}
|
|
515
|
639
|
|
|
516
|
640
|
/**
|