java后端获取支付宝的基本信息
- app绑定支付宝其实大部分都是由后端来完成的,绑定支付宝总共需要三步
- 第一步
public static String querySign() {
String token=System.currentTimeMillis()+new Random().nextInt()+"";
String targetId=null;
try {
MessageDigest md=MessageDigest.getInstance("md5");
byte[] md5=md.digest(token.getBytes());
BASE64Encoder base64Encoder = new BASE64Encoder();
targetId = base64Encoder.encode(md5);
System.out.println(targetId);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
StringBuffer asd=new StringBuffer();
SortedMap<String,String > map = new TreeMap<String,String >();
map.put("apiname","com.alipay.account.auth");
map.put("method","alipay.open.auth.sdk.code.get");
map.put("app_id", APP_ID);
map.put("app_name","app名称");
map.put("biz_type","openservice");
map.put("pid","app的pid");
map.put("product_id","APP_FAST_LOGIN");
map.put("scope","kuaijie");
map.put("target_id",targetId);
map.put("auth_type","AUTHACCOUNT");
map.put("sign_type","RSA");
String signStr = AlipaySignature.getSignContent(map);
String sign = null;
try {
sign = AlipaySignature.rsaSign(signStr,APP_PRIVATE_KEY, CHARSET);
String s=PayUtil.getSignEncodeUrl(map,true) + "&sign=" + URLEncoder.encode(sign,CHARSET);
String s1 = map.toString();
String replace = s1.replace(", ","&");
return replace.substring(1,replace.length()-1)+s;
} catch (AlipayApiException e) {
e.printStackTrace();
return "";
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "";
}
}
这一步执行成功后会返回
apiname=com.alipay.account.auth& app_id=*******& app_name=*****& auth_type=AUTHACCOUNT& biz_type=openservice& method=alipay.open.auth.sdk.code.get& pid=*******& product_id=APP_FAST_LOGIN& scope=kuaijie& sign_type=RSA& target_id=7UC8vDI78pXKaJLGKVTIJg==&sign=ZirsWxWPq9T6RvV65W9Vh1%2BEU1%2FywcSfz3HU4XlNb%2BLHY4LbhJ15%%2B1z0J%2F4yYBwxq4ehIodZdaxfwl8JXmA0hZlDNSERsMXWmDUUETE0gLAhFkKFJNO18928envJf%2B52vRaUNghvubxmILQ4UhIl2hLNnpKX8mxRZ3deAno1bCrs2E5Mfh8OC4CHSOUGtTWwPvlG0jDn9DrwY2kxpQ7VIrBKvKokyI7uTt5zBeZpWZA%3D%3D
app端(安卓,ios)都可以,拿这个数据与支付宝进行交互,来获取auth_code
- 第二步,拿auth_code与支付宝交互,来换取auth_token。
APP_PRIVATE_KEY 是支付宝公钥,上传应用私钥到支付宝平台支付宝自动生成
public static JSONObject findAuthToken(String auth_code) {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setCode(auth_code);
request.setGrantType("authorization_code");
try {
AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
return findUserInfo(oauthTokenResponse.getAccessToken());
} catch (AlipayApiException e) {
//处理异常
e.printStackTrace();
}
return null;
}
- 第三步,用auth_token交换用户的基本信息(不包括用户的手机号,用户账号)
public static JSONObject findUserInfo(String authToken){
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2");
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
try {
AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(request, "kuaijieB9eb8d912d2694957bf01d710b5bf5X69");
String body = userinfoShareResponse.getBody();
return JSONObject.parseObject(body);
} catch (AlipayApiException e) {
//处理异常
e.printStackTrace();
return null;
}
}
看了支付宝的官方文档也米看明白,最后在网上找到一篇帖子
地址 https://my.oschina.net/bddiudiu/blog/834607
希望帮助到大家。