Java实现支付宝小程序获取用户个人信息

4 篇文章 0 订阅
4 篇文章 0 订阅

Java实现支付宝用户授权,获取用户信息:

/***
 * 支付宝用户授权,获取用户信息
 * @author Mark
 * @param code
 * @return
 */
public static UserInfo getALiPayUserInfo(String code) {
    UserInfo info = null;
    //根据code获取accessToken
    AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
            "你的APPID", "你的支付宝私钥", "json",
            "GBK", "你的支付宝公钥", "RSA2");
    AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
    request.setGrantType("authorization_code");
    request.setCode(code);
    request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
    AlipaySystemOauthTokenResponse response = null;
    try {
        response = alipayClient.execute(request);
    } catch (AlipayApiException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (response.isSuccess()) {
    //根据accessToken获取用户信息
        AlipayClient alipayClients = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
"你的APPID", "你的支付宝私钥", "json",
            "GBK", "你的支付宝公钥", "RSA2");
        AlipayUserInfoShareRequest req = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse res = null;
        try {
            res = alipayClients.execute(req, response.getAccessToken());
        } catch (AlipayApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (res.isSuccess()) {
            info = new UserInfo();
            info.setUserId(res.getUserId());
            info.setAvatar(res.getAvatar());
            info.setProvince(res.getProvince());
            info.setCity(info.getCity());
            info.setNickName(res.getNickName());
            info.setIsStudentCertified(res.getIsStudentCertified());
            info.setUserType(res.getUserType());
            info.setUserStatus(res.getUserStatus());
            info.setIsCertified(res.getIsCertified());
            info.setGender(res.getGender());
            return info;
        } else {
            info = new UserInfo();
            return info;
        }
    } else {
        info = new UserInfo();
        return info;
    }

}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值