java获取支付宝用户信息

准备工作

1.在蚂蚁金服开发平台申请成为开发者
https://openhome.alipay.com/platform/home.htm
2.进入开发者中心创建应用
java获取支付宝用户信息_第1张图片

3.进入应用设置相应配置
java获取支付宝用户信息_第2张图片
生成密钥:
https://docs.open.alipay.com/291/106097
将生成的公钥设置到应用中
4.为应用创建功能
java获取支付宝用户信息_第3张图片java获取支付宝用户信息_第4张图片

搭建和配置开发环境

用户信息授权官方文档:https://docs.open.alipay.com/289/105656#s3

1.下载服务端SDK
https://docs.open.alipay.com/54/103419
java获取支付宝用户信息_第5张图片
2.参数设置

AlipayConfig.java

public class AlipayConfig {

    /**
     * 支付宝网关(固定)
     */
    public static final  String URL="https://openapi.alipay.com/gateway.do";

    /**
     * 授权url
     */
    public static final  String ALIPAY_URL = "https://openauth.alipay.com/oauth2/publicAppAuthorize.htm";

    /**
     * 应用id
     */
    public static final  String APP_ID="";

    /**
     * 应用私钥
     */
    public static final  String APP_PRIVATE_KEY="";

    /**
     * 参数返回格式
     */
    public static final  String FORMAT="json";

    /**
     * 编码集
     */
    public static final  String CHARSET="UTF-8";

    /**
     * 支付宝公钥
     */
    public static final  String ALIPAY_PUBLIC_KEY="";

    /**
     * 商户生成签名字符串所使用的签名算法类型
     */
    public static final  String SIGN_TYPE="RSA2";

}

3.开发授权功能
授权完整流程如下:
java获取支付宝用户信息_第6张图片
代码:
Controller:

    /**
     *  跳转到授权界面
     */
    @RequestMapping(value = "zhifubao")
    public String save(HttpServletRequest request,HttpServletResponse response) {
        Map maps = new HashMap();
        //页面回调地址 必须与应用中的设置一样
        String return_url = "http://127.0.0.1/zhifubao/imf";
        //回调地址必须经encode
        return_url = java.net.URLEncoder.encode(return_url);
        //重定向到授权页面
        return "redirect:"+AlipayConfig.ALIPAY_URL+"?app_id=" + AlipayConfig.APP_ID + "&scope=auth_user&redirect_uri=" + return_url;
    }
/**
     * 获取用户信息
     * @param request
     * @param response
     */
    @RequestMapping(value = "zhifubao/imf")
    public void returnImf(HttpServletRequest request, HttpServletResponse response) {
        //获取支付宝GET过来反馈信息
        Map params = new HashMap();
        Map requestParams = request.getParameterMap();
        for (Iterator iter = requestParams.keySet().iterator(); iter.hasNext();) {
            String name = (String) iter.next();
            String[] values = (String[]) requestParams.get(name);
            String valueStr = "";
            for (int i = 0; i < values.length; i++) {
                valueStr = (i == (values.length-1)) ? valueStr + values[i]:valueStr + values[i] + ",";
            }
            params.put(name, valueStr);
        }

        String accessToken= AlipaySubmit.buildRequest(params);
        if(accessToken!=null && accessToken!=""){
            String imf  =  AlipaySubmit.get(accessToken);
            System.out.println(imf);
        }
    }

AlipaySubmit.java

public class AlipaySubmit {

    /**
     * 获取AccessToken
     * @param sParaTemp
     * @return
     */
    public static  String buildRequest(Map sParaTemp){
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setCode((String)sParaTemp.get("auth_code"));
        request.setGrantType("authorization_code");
        try {
            AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
            System.out.println(oauthTokenResponse.getAccessToken());
            return oauthTokenResponse.getAccessToken();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取用户信息
     * @param accessToken
     * @return
     */
    public static String get(String accessToken){
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY, "json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, "RSA2");
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();

        try {
            AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(request, accessToken);
           return  userinfoShareResponse.getBody();
        } catch (AlipayApiException e) {
            //处理异常
            e.printStackTrace();
        }
        return null;
    }

}

直接访问http://127.0.0.1/zhifubao

注意

AlipayConfig.java中
public static final String ALIPAY_PUBLIC_KEY=”“;
设置的为支付宝公钥,并不是应用公钥,支付宝公钥在应用中获取,如下图
java获取支付宝用户信息_第7张图片

你可能感兴趣的:(java获取支付宝用户信息)