当前位置:网站首页>Alipay applet authorization / obtaining user information
Alipay applet authorization / obtaining user information
2022-07-28 02:21:00 【The sixth of June】
Alipay applet authorization / Get user information
Catalog :
List of articles
Get Alipay applet authorization token
Prerequisite preparation :
- Alipay applet sdk And so on .
- Create Alipay applet , And configure it according to the relevant process . Be careful : Only enterprise Alipay account can obtain user information permission
1、 rely on
<!-- Alipay core sdk -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.22.113.ALL</version>
</dependency>
<!-- Add / remove key sdk -->
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.62</version>
</dependency>
2、 Related process configuration
https://open.alipay.com/develop/manage
Wechat applet can log in after authorization through wechat , The platform can get the relevant information of wechat users . Similarly, Alipay applet can also .
technological process :
- Call the interface first / Method Get authorization token
- Re pass Interface / Method Get user information

1、 Get Alipay applet authorization token
This is equivalent to calling alipay.system.oauth.token Interface
// Server access access_token、user_id
private AlipaySystemOauthTokenResponse getAccessToken(String authCode) throws Exception {
String code = JSON.parseObject(authCode).getString("authCode");
// 1. fill appid
String APPID = "2021002147669716";
// 2. Fill in the application private key
String PRIVATE_KEY = " Application of the private key ";
// 3. Fill in the public key of Alipay
String ALIPAY_PUBLIC_KEY = " 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 way :authorization_code, It means to use the user authorization code in exchange code Exchange authorization token access_token.
request.setGrantType("authorization_code");
// 4. Fill in the authorization code passed in by the front end authCode
// Authorization code , After the user authorizes the application, he gets . This parameter is in grant_type by authorization_code Required when
request.setCode(code);
AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println(" Successful call ");
} else {
System.out.println(" Call failed ");
}
return response;
}

2、 Obtain basic information of authorized users
Let's call the interface alipay.user.info.auth
// Get Alipay user information
private AlipayUserInfoShareResponse getAliUserInfo (String accessToken) throws Exception {
// 1. fill appid
String APPID = "2021002147669716";
// 2. Fill in the application private key
String PRIVATE_KEY = " Application of the private key ";
// 3. Fill in the public key of Alipay
String ALIPAY_PUBLIC_KEY = " 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(" Get member information - Successful call ");
return response;
}
return null;
}
Problems encountered :
【 Alipay applet 】-【 Get Alipay user details 】-【 Respond to code:20001】-【 error message : Invalid access token 】
solve :


Parse Alipay applet interface to respond to encrypted data
Analyze the interface response encrypted data flow :
- Get the parameters required for signature verification and decryption
- attestation
- Decrypt

1、 Parsing interface response encrypted data
/** * Decrypt data * @return */
@ApiOperation(value = " Decrypt data ")
@PostMapping("/findPhone")
public String findPhone(@RequestBody String jsonStr) throws Exception {
// Encrypted data submitted by the front end of the applet
String response = JSON.parseObject(jsonStr).getString("response");
//1. Get the parameters required for signature verification and decryption
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");
// If the ciphertext
boolean isDataEncrypted = !content.startsWith("{");
boolean signCheckPass = false;
//2. attestation
String signContent = content;
// The public key of Alipay corresponding to your applet ( For expansion, it is suggested to use appId+signType Do key storage isolation )
String signVeriKey = "";
// The encryption and decryption key corresponding to your applet ( For expansion, it is suggested to use appId+encryptType Do key storage isolation )
String decryptKey = "";
// If it is an encrypted message, you need to add double quotation marks before and after the ciphertext
if (isDataEncrypted) {
signContent = "\"" + signContent + "\"";
}
try {
signCheckPass = AlipaySignature.rsaCheck(signContent, sign, signVeriKey, charset, signType);
} catch (AlipayApiException e) {
// Abnormal attestation , journal
}
if(!signCheckPass) {
// Signature verification failed ( Exception or message is tampered ), Terminate the process ( There is no need to decrypt )
throw new Exception(" Attestation of failure ");
}
//3. Decrypt
String plainData = null;
if (isDataEncrypted) {
try {
plainData = AlipayEncrypt.decryptContent(content, encryptType, decryptKey, charset);
} catch (AlipayApiException e) {
// Decryption abnormal , Log
throw new Exception(" Decryption abnormal ");
}
} else {
plainData = content;
}
return plainData;
}

边栏推荐
- The principle and implementation of loss function cross entropy
- 一种比读写锁更快的锁,还不赶紧认识一下
- Promise从入门到精通 (第2章 Promise的理解和使用)
- Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
- Vxe table/grid cell grouping and merging
- 【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
- 【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
- Codeforces Round #810 (Div. 2)A~C题解
- They are all talking about Devops. Do you really understand it?
- Flex开发网页实例web端
猜你喜欢

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

考研数学一元微分学证明题常见题型方法

Redis design specification

结构伪类选择器—查找单个—查找多个—nth-of-type和伪元素

Structure pseudo class selector - find single - find multiple - nth of type and pseudo elements

Promise从入门到精通(第3章 自定义(手写)Promise)

Appium click operation sorting

正则表达式

LeetCode 热题 HOT 100 -> 1.两数之和

产品解读丨MeterSphere UI测试模块的设计与分布式扩展
随机推荐
CeresDAO:Ventures DAO的“新代言”
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
synchronized详解
【ROS进阶篇】第十讲 基于Gazebo的URDF集成仿真流程及实例
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
[website construction] update SSL certificate with acme.sh: change zerossl to letsencrypt
数据输出-图片注释、标注
Common problem types and methods of mathematical univariate differential proof problems in postgraduate entrance examination
54: Chapter 5: develop admin management services: 7: face warehousing process; Face login process; The browser turns on the video debugging mode (so that the camera can also be turned on in the case o
[Star Project] small hat aircraft War (VI)
交叉熵原理及实现
Software test interview question: please introduce the meaning of various test types in detail?
【Star项目】小帽飞机大战(五)
Software testing interview question: what types of software testing are you familiar with?
uniapp 总结篇 (小程序)
CeresDAO:全球首个基于DAO赋能Web3.0的去中心化数字资产管理协议
[database data recovery] data recovery case of insufficient disk space of SQL Server database
Flex布局—固定定位+流式布局—主轴对齐—侧轴对齐—伸缩比
【愚公系列】2022年07月 Go教学课程 019-循环结构之for
Principle and implementation of cross entropy