前言:
最近公司做一个支付宝小程序项目,用支付宝userId做唯一用户id,后台encryptedData解密出用户支付宝绑定的手机号信息,其中
正文开始:
贴代码:
1.authtoken获取userId 前端文档 后端文档
public String findUserId(String authCode) throws AdminException, AlipayApiException {
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.url, AlipayConfig.app_id, AlipayConfig.private_key, AlipayConfig.format, AlipayConfig.charset, AlipayConfig.public_key, AlipayConfig.signtype);
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(authCode);
// request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
//String accessToken = response.getAccessToken();
if (response.isSuccess()) {
//log.info("调用成功");
//log.info("支付宝用户唯一id:" + response.getUserId());
// log.info("token令牌:" + response.getAccessToken()); //访问令牌。通过该令牌调用需要授权类接口
return response.getUserId();
}
return null;
}
其中的accessToken我没有用到,你们用到了解除注释就可以,亲测可以获取到。
2.encryptedData解密手机号:
//解密手机号
JSONObject jsonObject =JSONObject.parseObject(userSmallLoginRequest.getEncryptedData());
String phoneResult = AESCBCUtil.RealDecrypt(jsonObject.getString("response"), AlipayConfig.aesSecretKey);
JSONObject jsonObject1 = JSONObject.parseObject(phoneResult);
if(!"10000".equals(jsonObject1.getString("code"))){
throw new AdminException("用户手机号解密失败");
}
String phone = jsonObject1.getString("mobile");
其中的AlipayConfig.aesSecretKey是支付宝小程序设置的密钥
本文暂时没有评论,来添加一个吧(●'◡'●)