当前位置:网站首页>支付宝小程序授权/获取用户信息
支付宝小程序授权/获取用户信息
2022-07-28 01:01:00 【六月的六】
支付宝小程序授权/获取用户信息
目录:
获取支付宝小程序授权token
前提准备工作:
- 支付宝小程序sdk等相关依赖。
- 创建支付宝小程序,并按照相关流程配置好。注意:必须是企业支付宝账号才可以获取用户信息权限
1、依赖
<!-- 支付宝核心sdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.22.113.ALL</version>
</dependency>
<!-- 加解密钥sdk -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.62</version>
</dependency>
2、相关流程配置
https://open.alipay.com/develop/manage
微信小程序可以通过微信授权之后再登录,平台可以拿到微信用户的相关信息。同理支付宝小程序也可以。
流程:
- 先调用接口/方法 获取授权token
- 再通过 接口/方法 获取用户信息

1、获取支付宝小程序授权token
这里相当于调用 alipay.system.oauth.token接口
// 服务端获取access_token、user_id
private AlipaySystemOauthTokenResponse getAccessToken(String authCode) throws Exception {
String code = JSON.parseObject(authCode).getString("authCode");
// 1. 填入appid
String APPID = "2021002147669716";
// 2. 填入应用私钥
String PRIVATE_KEY = "应用私钥";
// 3. 填入支付宝公钥
String ALIPAY_PUBLIC_KEY = "支付宝公钥";
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
APPID,
PRIVATE_KEY,
"json",
"GBK",
ALIPAY_PUBLIC_KEY,
"RSA2");
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
//授权方式:authorization_code,表示换取使用用户授权码code换取授权令牌access_token。
request.setGrantType("authorization_code");
// 4. 填入前端传入的授权码authCode
//授权码,用户对应用授权后得到。本参数在 grant_type 为 authorization_code 时必填
request.setCode(code);
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}
return response;
}

2、获得授权用户的基本信息
我们再调用接口 alipay.user.info.auth
// 获取支付宝用户信息
private AlipayUserInfoShareResponse getAliUserInfo (String accessToken) throws Exception {
// 1. 填入appid
String APPID = "2021002147669716";
// 2. 填入应用私钥
String PRIVATE_KEY = "应用私钥";
// 3. 填入支付宝公钥
String ALIPAY_PUBLIC_KEY = "支付宝公钥";
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
APPID ,
PRIVATE_KEY,
"json",
"GBK",
ALIPAY_PUBLIC_KEY,
"RSA2");
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = alipayClient.execute(request, accessToken);
if(response.isSuccess()){
System.out.println("获取会员信息 - 调用成功");
return response;
}
return null;
}
遇到的问题:
【支付宝小程序】-【获得支付宝用户详情】-【响应code:20001】-【错误信息:无效的访问令牌】
解决:


解析支付宝小程序接口响应加密数据
解析接口响应加密数据流程:
- 获取验签和解密所需要的参数
- 验签
- 解密

1、解析接口响应加密数据
/** * 解密数据 * @return */
@ApiOperation(value = "解密数据")
@PostMapping("/findPhone")
public String findPhone(@RequestBody String jsonStr) throws Exception {
//小程序前端提交的加密数据
String response = JSON.parseObject(jsonStr).getString("response");
//1. 获取验签和解密所需要的参数
Map<String, String> openapiResult = JSON.parseObject(response,
new TypeReference<Map<String, String>>() {
}, Feature.OrderedField);
String signType = "RSA2";
String charset = "UTF-8";
String encryptType = "AES";
String sign = openapiResult.get("sign");
String content = openapiResult.get("response");
//如果密文的
boolean isDataEncrypted = !content.startsWith("{");
boolean signCheckPass = false;
//2. 验签
String signContent = content;
//你的小程序对应的支付宝公钥(为扩展考虑建议用appId+signType做密钥存储隔离)
String signVeriKey = "";
//你的小程序对应的加解密密钥(为扩展考虑建议用appId+encryptType做密钥存储隔离)
String decryptKey = "";
//如果是加密的报文则需要在密文的前后添加双引号
if (isDataEncrypted) {
signContent = "\"" + signContent + "\"";
}
try {
signCheckPass = AlipaySignature.rsaCheck(signContent, sign, signVeriKey, charset, signType);
} catch (AlipayApiException e) {
//验签异常, 日志
}
if(!signCheckPass) {
//验签不通过(异常或者报文被篡改),终止流程(不需要做解密)
throw new Exception("验签失败");
}
//3. 解密
String plainData = null;
if (isDataEncrypted) {
try {
plainData = AlipayEncrypt.decryptContent(content, encryptType, decryptKey, charset);
} catch (AlipayApiException e) {
//解密异常, 记录日志
throw new Exception("解密异常");
}
} else {
plainData = content;
}
return plainData;
}

边栏推荐
猜你喜欢

54:第五章:开发admin管理服务:7:人脸入库流程;人脸登录流程;浏览器开启视频调试模式(以便能够在本机的不安全域名的情况下,也能去开启摄像头);

CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议

Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比

产品解读丨MeterSphere UI测试模块的设计与分布式扩展

In it, there is a million talent gap, and the salary rises, but it is not capped

uniapp 总结篇 (小程序)

Unity 保存图片到相册以及权限管理

智能合约安全——selfdestruct攻击

小程序毕设作品之微信校园浴室预约小程序毕业设计成品(1)开发概要

Execute add migration migration and report build failed
随机推荐
LeetCode 热题 HOT 100 -> 2.两数相加
Typescript中类的使用
WMS you don't know
Flume(5个demo轻松入门)
mongodb/mongoTemplate.upsert批量插入更新数据的实现
Vxe Table/Grid 单元格分组合并
Synchronized details
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
Use of classes in typescript
Product interpretation - Design and distributed expansion of metersphere UI test module
Talk to ye Yanxiu, an atlassian certification expert: where should Chinese users go when atlassian products enter the post server era?
软件测试面试题:请详细介绍一下各种测试类型的含义?
The principle and implementation of loss function cross entropy
11 Django basics database operation
探究flex-basis
Implementation of mongodb/mongotemplate.upsert batch inserting update data
软件测试面试题:你认为做好测试用例设计工作的关键是什么?
Flume (5 demos easy to get started)
Promise from introduction to mastery (Chapter 2 understanding and use of promise)
埃睿迪再度亮相数字中国峰会 持续深化用科技守护绿水青山