阿里云文档:号码认证SDK_号码认证服务(PNVS)-阿里云帮助中心
对于后端大概流程 前端App会传一个token过来 后端通过下面方法解析 如果解析可以获得号码,说明号码认证成功,如果无法正确解析则认证失败
/**
* actoken来换取电话号码
* @param token app端用户授权actoken
* @return
*/
public static String getPhone(String token) {
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyID, accessKeySecret);//自己阿里云的配置
IAcsClient client =new DefaultAcsClient(profile);
GetMobileRequest request = new GetMobileRequest();
request.setAccessToken(token);//app端传过来的,需要用户授权拿到
request.setSysRegionId(regionId);
String phone = null;
try {
GetMobileResponse response = client.getAcsResponse(request);
if("OK".equals(response.getCode())) {
System.out.println(response.toString());
phone = response.getGetMobileResultDTO().getMobile();
};
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
return phone;
}
依赖
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.4.4</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-dypnsapi</artifactId> <version>1.0.4</version> </dependency>