当前位置:   article > 正文

支付宝生活号对接-----(一)授权_支付宝 生活号 测试账号

支付宝 生活号 测试账号

第一次做支付宝生活号, 一脸蒙蔽, 在同事的帮助下勉强做出来了,下面分享一下沙箱环境的应用,正式和沙箱是一样的

因为我是直接有了支付宝生活号,并且已经添加了应用 ,所以我直接从配置开始说起

在按照下面步骤做之前 最好先了解一下支付宝授权过程,

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进入沙箱环境

这时候可以看到上面访问需要授权的很多配置

这些配好了就可以准备粘贴代码了

  1. public class AlipayServiceEnvConstants2 {
  2. /**支付宝公钥-从支付宝生活号详情页面获取*/
  3. public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAquvU1bcCLHpSjnEq9s9diJGTkb5QLwGpJSDxUtQI5+D2RrvouJJsTQC/iueK02kPhEsO9V7qSK2RI74nEc6IMntnC5npj0InfsX6nAJpjZBVR03qfJeWeE1aJ6aD7AvPSrZWGHGKLU7eo2OU0DnDAje/aHZc6cIQrUWDPbP+y/bKF1bU7Q3aLrUF8/MnGBtHZ+uPjkeIMy9FXBWcwn6U8GkFaomoJX9Pv/C94Mh3oxWRd3eernc0Vk4ZJHVEksNIB9ffKXxWgXvpaiXjxZoperxoDXDkh/P/32KCV4GFEoU9wMN8Qb7smKLS8R+EzmdoaFVKoLP9AZWg5luZmBAOyQIDAQAB";
  4. /**签名编码-视支付宝服务窗要求*/
  5. public static final String SIGN_CHARSET = "GBK";
  6. /**字符编码-传递给支付宝的数据编码*/
  7. public static final String CHARSET = "GBK";
  8. /**签名类型-视支付宝服务窗要求*/
  9. public static final String SIGN_TYPE = "RSA2";
  10. /**开发者账号PID*/
  11. public static final String PARTNER = "2088621459192115";
  12. /** 服务窗appId */
  13. //TODO !!!! 注:该appId必须设为开发者自己的生活号id
  14. public static final String APP_ID = "2016080300159887";
  15. //私钥
  16. 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=";
  17. // //TODO !!!! 注:该公钥为测试账号公钥 开发者必须设置自己的公钥 ,否则会存在安全隐患
  18. // public static final String PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjwXDOBfcKi27DElUCZHzBR0pP5ufIoYi1l9yEx/RduqAbyl3NKM3UMBhgiE4Z2C3hoHjaoObHKldtD3BZHohWmgSXdk4BpLRgm4GkqRjMunp/QXkoSIJelPfRZS5289pFy1Fp7Ekia/9LhKDNMuHD84B3rfOhxiQkkuhg6lnWnjg3JWfMcsDFiP+/b/7uqkG7De0MHVe30uqgmY6mgQkdvwsiU845upRZGlzTcUSpcGYqZFH001oohCLu5A+zgKvMVUU+A2T9ADAMgguhgw08jSelVew+pPxPZ4rQntWxclU32ElL1aHWaY3f3syz3pnHxHk7c2gCkRPRgNsnrv36wIDAQAB";
  19. /**支付宝网关*/
  20. public static final String ALIPAY_GATEWAY = "https://openapi.alipaydev.com/gateway.do";
  21. /**授权访问令牌的授权类型*/
  22. public static final String GRANT_TYPE = "authorization_code";
  23. }
  1. @RestController
  2. @RequestMapping("/oauth")
  3. @Api(value="支付宝授权回调")
  4. public class AliPayOauth2Controller {
  5. Logger BAITIAO_USER_LOG = LoggerUtils.Logger(LogFileName.BAITIAO_USER);
  6. @RequestMapping(value = "/getAuthCode" )
  7. @ResponseBody
  8. public String resultCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
  9. String code = null;
  10. String openid = null;
  11. String state= request.getParameter("state");
  12. code = request.getParameter("auth_code");// 用户授权码
  13. BAITIAO_USER_LOG.info("自定义参数为==="+state);
  14. // 沙箱环境
  15. AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipaydev.com/gateway.do",
  16. AlipayServiceEnvConstants2.APP_ID,
  17. AlipayServiceEnvConstants2.PRIVATE_KEY, "json", AlipayServiceEnvConstants2.CHARSET, AlipayServiceEnvConstants2.ALIPAY_PUBLIC_KEY, "RSA2");
  18. //正式环境
  19. /*AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
  20. AlipayServiceEnvConstants.APP_ID,
  21. AlipayServiceEnvConstants.PRIVATE_KEY, "json", AlipayServiceEnvConstants.CHARSET, AlipayServiceEnvConstants.ALIPAY_PUBLIC_KEY, "RSA2");*/
  22. AlipaySystemOauthTokenRequest tokenRequest = new AlipaySystemOauthTokenRequest();
  23. tokenRequest.setCode(code);
  24. tokenRequest.setGrantType(AlipayServiceEnvConstants.GRANT_TYPE);
  25. try {
  26. AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(tokenRequest);
  27. System.out.println(oauthTokenResponse.getAccessToken());
  28. openid = oauthTokenResponse.getUserId();
  29. } catch (AlipayApiException e) {
  30. //处理异常
  31. e.printStackTrace();
  32. }
  33. String url=state+"?openId="+openid;
  34. BAITIAO_USER_LOG.info("openId==="+openid);
  35. BAITIAO_USER_LOG.info("拼接路径为==="+url);
  36. return "redirect:url";
  37. //return openid;
  38. }
  39. }

上面的配置文件 有注释可以自己看, 下面是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依赖

  1. <dependency>
  2. <groupId>com.alipay.sdk</groupId>
  3. <artifactId>alipay-sdk-java</artifactId>
  4. <version>3.6.0.ALL</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>commons-logging</groupId>
  8. <artifactId>commons-logging</artifactId>
  9. <version>1.1.1</version>
  10. </dependency>

controller 不报错就算可以了

然后运行, 用授权url 尝试, 就可以了

如果让登录  请用沙箱账号登录 

剩下的就看你自己了,或者请教老同事

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/524369
推荐阅读
相关标签
  

闽ICP备14008679号