当前位置:网站首页>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;
}

边栏推荐
- Flex layout - fixed positioning + flow layout - main axis alignment - side axis alignment - expansion ratio
- Redis design specification
- 【愚公系列】2022年07月 Go教学课程 019-循环结构之for
- Shell regular and metacharacters
- 微信小程序图片根据屏幕比例缩放
- Understand the "next big trend" in the encryption industry - ventures Dao
- Flume(5个demo轻松入门)
- 数据输出-图片注释、标注
- 软考 --- 数据库(2)关系模型
- Leetcode hot topic Hot 100 - > 2. Add two numbers
猜你喜欢

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

Traversal and properties of binary trees

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

IT这个岗位,人才缺口百万,薪资水涨船高,上不封顶

MySQL高可用和主从同步

QGIS mapping: vector data mapping process and export

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

LeetCode 热题 HOT 100 -> 3. 无重复字符的最长子串

测试/开发程序员的级别“陷阱“,级别不是衡量单维度的能力......

CeresDAO:Ventures DAO的“新代言”
随机推荐
Appium 点击操作梳理
软考 --- 数据库(2)关系模型
[Star Project] small hat aircraft War (VI)
[深入研究4G/5G/6G专题-42]: URLLC-14-《3GPP URLLC相关协议、规范、技术原理深度解读》-8-低延时技术-2-基于slot的调度与Slot内灵活的上下行符号配比
四种常见的 POST 提交数据方式
清除浮动的原因和六种方法(解决浮动飞起影响父元素和全局的问题)
Ceresdao: the world's first decentralized digital asset management protocol based on Dao enabled Web3.0
In it, there is a million talent gap, and the salary rises, but it is not capped
Appium click operation sorting
Clear the cause of floating and six methods (solve the problem that floating affects the parent element and the global)
【数据库数据恢复】SQL Server数据库磁盘空间不足的数据恢复案例
借助Elephant&nbsp;Swap打造的ePLATO,背后的高溢价解析
Promise从入门到精通 (第2章 Promise的理解和使用)
【Star项目】小帽飞机大战(六)
MySQL pymysql operation
[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
小程序毕设作品之微信校园浴室预约小程序毕业设计成品(3)后台功能
Software testing interview question: what do you think is the key to good test case design?
Leetcode hot topic Hot 100 - > 1. Sum of two numbers