赞
踩
第一次做支付宝生活号, 一脸蒙蔽, 在同事的帮助下勉强做出来了,下面分享一下沙箱环境的应用,正式和沙箱是一样的
因为我是直接有了支付宝生活号,并且已经添加了应用 ,所以我直接从配置开始说起
在按照下面步骤做之前 最好先了解一下支付宝授权过程,
https://docs.open.alipay.com/263/105809/
这个上面有详细的介绍
下面的步骤与介绍基本一致, 不过详细一点
下面所有的步骤只为了支付宝授权 获取userId
通过地址调用 来进行静默授权或者非静默授权
拼接授权页面url: 以及参数说明
正式环境:
https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id=商户的APPID&scope=auth_user&redirect_uri=ENCODED_URL&state=init
沙箱环境:
https://openapi.alipaydev.com/oauth2/publicAppAuthorize.htm?app_id=商户的APPID&scope=auth_user&redirect_uri=ENCODED_URL&state=init
另外注意网关:
切换沙箱环境时,除了支付宝网关https://openapi.alipay.com/gateway.do需要切换成https://openapi.alipaydev.com/gateway.do之外,还需要将
切换沙箱环境时,除了支付宝网关https://openapi.alipay.com/gateway.do需要切换成https://openapi.alipaydev.com/gateway.do之外,还需要将
第一步:创建应用
此步骤省略
https://docs.open.alipay.com/291/105971/
下载生成密钥以及公钥的工具
生成之后,
https://openhome.alipay.com/platform/appDaily.htm j进入沙箱环境
这时候可以看到上面访问需要授权的很多配置
这些配好了就可以准备粘贴代码了
- public class AlipayServiceEnvConstants2 {
-
- /**支付宝公钥-从支付宝生活号详情页面获取*/
- public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAquvU1bcCLHpSjnEq9s9diJGTkb5QLwGpJSDxUtQI5+D2RrvouJJsTQC/iueK02kPhEsO9V7qSK2RI74nEc6IMntnC5npj0InfsX6nAJpjZBVR03qfJeWeE1aJ6aD7AvPSrZWGHGKLU7eo2OU0DnDAje/aHZc6cIQrUWDPbP+y/bKF1bU7Q3aLrUF8/MnGBtHZ+uPjkeIMy9FXBWcwn6U8GkFaomoJX9Pv/C94Mh3oxWRd3eernc0Vk4ZJHVEksNIB9ffKXxWgXvpaiXjxZoperxoDXDkh/P/32KCV4GFEoU9wMN8Qb7smKLS8R+EzmdoaFVKoLP9AZWg5luZmBAOyQIDAQAB";
-
- /**签名编码-视支付宝服务窗要求*/
- public static final String SIGN_CHARSET = "GBK";
-
- /**字符编码-传递给支付宝的数据编码*/
- public static final String CHARSET = "GBK";
-
- /**签名类型-视支付宝服务窗要求*/
- public static final String SIGN_TYPE = "RSA2";
-
- /**开发者账号PID*/
- public static final String PARTNER = "2088621459192115";
-
- /** 服务窗appId */
- //TODO !!!! 注:该appId必须设为开发者自己的生活号id
- public static final String APP_ID = "2016080300159887";
-
- //私钥
- public static final String PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCPBcM4F9wqLbsMSVQJkfMFHSk/m58ihiLWX3ITH9F26oBvKXc0ozdQwGGCIThnYLeGgeNqg5scqV20PcFkeiFaaBJd2TgGktGCbgaSpGMy6en9BeShIgl6U99FlLnbz2kXLUWnsSSJr/0uEoM0y4cPzgHet86HGJCSS6GDqWdaeODclZ8xywMWI/79v/u6qQbsN7QwdV7fS6qCZjqaBCR2/CyJTzjm6lFkaXNNxRKlwZipkUfTTWiiEIu7kD7OAq8xVRT4DZP0AMAyCC6GDDTyNJ6VV7D6k/E9nitCe1bFyVTfYSUvVodZpjd/ezLPemcfEeTtzaAKRE9GA2yeu/frAgMBAAECggEAZeUCH12IYphwEaHQeHz9uZ/7+jBCINOV0Erxt8RMdD8/v3K4Vc0OpL5a+d33FFeoiPDS99jJUpMrijURKieEyacppqkSLES/dQA0OJdbgTN7m+Nt7oqUXA2THTyHaZ4MThHX9U/UKzAx3Xgc+CkkkVMArA8h2cfbgA7YBDhVxPMV/Xjm2RKygLR7PWpUbnlUveDienQgr+Gy0IOhqXbMFxfGGjDvsMEjbC0Oxn8DjN/zINAtbXNmcMc0UVyK07af4eziDgUH66aVpDT1dXwPYtbfHwGkOW9bnSwHc0hn/+xg4zDYW3SdfJ2CivTpTjbUQ8aCdbnBK88dKsRLcgI1sQKBgQDQnr49RtykHctsmuqzUrycsrA0C2Oe3lSZiolD1r0rn2KPAEt3kjXi2uOnYtwPcc18b+m6FgwyO4k1M38o8ldwdcXrh0hFmBb9GV+I2Ef8nJ5PD4xC5gQl6WZX0c1PnR70UKxQScmWVIlTmKeUnR2LJ0Ki47OCcNll2wZQJUawCQKBgQCvgSZDOCYQCs0mCji56cDaO5qXBHWf+90qvIlEZN70KRIPyYJgfSiDilClYY4L0/1ovsgz55xZX+mtkDFu5fPJErMUhiGndnEgcAZYdK/mu8lB7moQbsdlR3eHH2F8WmMxveB43uwDtxpRYuEVGOTlsPeEbLGME2M9zLqizBj9UwKBgCW3DR122hyxZj1UJGoW4xJRG5nNFU17VEAN05L4F2rzfZXsr7gGLse8WrvAG5iPMl70vIRvx/Ss3rlHfg2hAj3KjOVvp5ktTbqkA6lxsFHy3ovce7b8bnOMJnxp6XwjI513mC9Lp64NymXaizFK1jR5/IoGMEW8sRDA7/s9QfJBAoGBAIXTMA7voK543geUob65zJFcA7wF7X9JeVLeIrlhRVTegGRDXsQei6cJ8Aa7hanlQwT8/QH/KQxJg5ESM2+BrsvzE1SiFsVwHkECywPmLj79kDBTENNo9Wh3/1+sgRRmg97pD2J7cXI/y7SldkY1Dk1LDOIQU64hFnqIQwRiJuf7AoGAIXT20R5Kz/O+X5W41G2bTF2IbeuYtR2YVBvti1uqoLaVYhiegYoQWUSkkLFk4P6S0qN3D5RrJiAYTXzgvbmP+eyJZphvZhxUM5wg/K40QBad2IPoANSRczWWkb9PABcYHh5CiQrBDSfHodtCtwDZjWOVFM65kV4tr8WMwBHXGhA=";
-
- // //TODO !!!! 注:该公钥为测试账号公钥 开发者必须设置自己的公钥 ,否则会存在安全隐患
- // public static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjwXDOBfcKi27DElUCZHzBR0pP5ufIoYi1l9yEx/RduqAbyl3NKM3UMBhgiE4Z2C3hoHjaoObHKldtD3BZHohWmgSXdk4BpLRgm4GkqRjMunp/QXkoSIJelPfRZS5289pFy1Fp7Ekia/9LhKDNMuHD84B3rfOhxiQkkuhg6lnWnjg3JWfMcsDFiP+/b/7uqkG7De0MHVe30uqgmY6mgQkdvwsiU845upRZGlzTcUSpcGYqZFH001oohCLu5A+zgKvMVUU+A2T9ADAMgguhgw08jSelVew+pPxPZ4rQntWxclU32ElL1aHWaY3f3syz3pnHxHk7c2gCkRPRgNsnrv36wIDAQAB";
- /**支付宝网关*/
- public static final String ALIPAY_GATEWAY = "https://openapi.alipaydev.com/gateway.do";
-
- /**授权访问令牌的授权类型*/
- public static final String GRANT_TYPE = "authorization_code";
- }
- @RestController
- @RequestMapping("/oauth")
- @Api(value="支付宝授权回调")
- public class AliPayOauth2Controller {
- Logger BAITIAO_USER_LOG = LoggerUtils.Logger(LogFileName.BAITIAO_USER);
- @RequestMapping(value = "/getAuthCode" )
- @ResponseBody
- public String resultCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
- String code = null;
- String openid = null;
- String state= request.getParameter("state");
- code = request.getParameter("auth_code");// 用户授权码
- BAITIAO_USER_LOG.info("自定义参数为==="+state);
- // 沙箱环境
-
- AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
- AlipayServiceEnvConstants2.APP_ID,
- AlipayServiceEnvConstants2.PRIVATE_KEY, "json", AlipayServiceEnvConstants2.CHARSET, AlipayServiceEnvConstants2.ALIPAY_PUBLIC_KEY, "RSA2");
- //正式环境
- /*AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
- AlipayServiceEnvConstants.APP_ID,
- AlipayServiceEnvConstants.PRIVATE_KEY, "json", AlipayServiceEnvConstants.CHARSET, AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY, "RSA2");*/
-
- AlipaySystemOauthTokenRequest tokenRequest = new AlipaySystemOauthTokenRequest();
- tokenRequest.setCode(code);
- tokenRequest.setGrantType(AlipayServiceEnvConstants.GRANT_TYPE);
- try {
- AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(tokenRequest);
- System.out.println(oauthTokenResponse.getAccessToken());
- openid = oauthTokenResponse.getUserId();
- } catch (AlipayApiException e) {
- //处理异常
- e.printStackTrace();
- }
- String url=state+"?openId="+openid;
- BAITIAO_USER_LOG.info("openId==="+openid);
- BAITIAO_USER_LOG.info("拼接路径为==="+url);
- return "redirect:url";
- //return openid;
- }
-
- }
上面的配置文件 有注释可以自己看, 下面是controller的写法 可以获取到userId了
我写的是openId
如果openId和UID 一样就说明成功了
当然还要引入这些JAR包
https://docs.open.alipay.com/54/103419/
下载jar包到本地引入仓库
打开cmd 窗口执行:
mvn install:install-file -DgroupId=com.alipay.sdk -DartifactId=alipay-sdk-java -Dversion=3.6.0.ALL -Dpackaging=jar -Dfile=F:\java\alipay-sdk-java-3.6.0.ALL.jar
记得把 F:\java\alipay-sdk-java-3.6.0.ALL.jar 这个地址换成自己的就好了
然后引入pom依赖
- <dependency>
- <groupId>com.alipay.sdk</groupId>
- <artifactId>alipay-sdk-java</artifactId>
- <version>3.6.0.ALL</version>
- </dependency>
-
- <dependency>
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- </dependency>
controller 不报错就算可以了
然后运行, 用授权url 尝试, 就可以了
如果让登录 请用沙箱账号登录
剩下的就看你自己了,或者请教老同事
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。