Java源码示例:com.alipay.api.response.AlipayUserInfoShareResponse
示例1
@Override
public AlipayUserInfo getUserInfo() {
AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, properties.getPrivateKey(), properties.getFormat(), properties.getCharset(), properties.getPublicKey(), properties.getSignType());
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = null;
try {
response = alipayClient.execute(request, this.accessToken);
if (response.isSuccess()) {
AlipayUserInfo alipayUserInfo = new AlipayUserInfo();
alipayUserInfo.setAvatar(response.getAvatar());
alipayUserInfo.setNickName(response.getNickName());
alipayUserInfo.setUserId(response.getUserId());
return alipayUserInfo;
} else {
throw new IllegalArgumentException(response.getMsg());
}
} catch (AlipayApiException e) {
throw new IllegalArgumentException(e.getMessage());
}
}
示例2
@Override
protected AuthUser getUserInfo(AuthToken authToken) {
String accessToken = authToken.getAccessToken();
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = null;
try {
response = this.alipayClient.execute(request, accessToken);
} catch (AlipayApiException e) {
throw new AuthException(e.getErrMsg(), e);
}
if (!response.isSuccess()) {
throw new AuthException(response.getSubMsg());
}
String province = response.getProvince(), city = response.getCity();
String location = String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city);
return AuthUser.builder()
.rawUserInfo(JSONObject.parseObject(JSONObject.toJSONString(response)))
.uuid(response.getUserId())
.username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName())
.nickname(response.getNickName())
.avatar(response.getAvatar())
.location(location)
.gender(AuthUserGender.getRealGender(response.getGender()))
.token(authToken)
.source(source.toString())
.build();
}
示例3
/**
* 支付宝普通授权(获取用户信息)的回调方法
* 由支付宝服务器调用
*
* @param rc Vertx的RoutingContext对象
* @author Leibniz.Hu
*/
private void oauthInfoCallback(RoutingContext rc) {
HttpServerRequest req = rc.request();
HttpServerResponse resp = rc.response();
Integer eid = Integer.parseInt(req.getParam("eid"));
getAccountAndExecute(resp, eid, aliAcc -> {
AlipayUserInfoShareResponse oauthRes = AliPayApi.getUserDetailInfo(aliAcc, req);
oauthSuccessProcess(req, resp, oauthRes, url -> log.info("授权成功,OpenID={},准备跳转到{}", oauthRes.getUserId(), url));
});
}
示例4
public Class<AlipayUserInfoShareResponse> getResponseClass() {
return AlipayUserInfoShareResponse.class;
}
示例5
/**
* 该方法是用户确定授权后的获取用户信息的方法;
* 方法先解析请求参数,获取auth_code;
* 获取到auth_code后,用auth_code换取auth_token和openId;
* 然后,判断用户是否需要获取用户详细信息,如果需要,则用auth_token获取用户详细信息,并返回AlipayUserInfoShareResponse响应对象;
* 如果不需要,直接返回AlipaySystemOauthTokenResponse响应对象;
* 其他的意外情况均返回null;
*
* @param aliAccountInfo 封装了获取用户详细信息需要用到的数据的对象
* @param isNeedDetail 是否需要获取详细信息的标识
* @param request http请求体
* @return AlipayResponse类型,调用者根据是否需要获取详细信息的标识强转成不同类型的对象,false强转成AlipaySystemOauthTokenResponse,true强转成AlipayUserInfoShareResponse,意外情况返回空
* Create by quandong
*/
private static AlipayResponse getUserInfo(AliAccountInfo aliAccountInfo, boolean isNeedDetail, HttpServerRequest request) {
Map<String, String> params = AliPayApi.getRequestParams(request); // 解析请求参数
String authCode = params.get("auth_code"); // 获得authCode
try {
// 判断是否能获取到authCode
if(null != authCode && !authCode.equals("")) { // 能获取到authCode
AlipayClient alipayClient = AliPayCliFactory.getAlipayClient(aliAccountInfo); // 获取支付宝连接
// 利用authCode获得authToken
AlipaySystemOauthTokenRequest oauthTokenRequest = new AlipaySystemOauthTokenRequest(); // 创建支付宝系统授权token请求对象
oauthTokenRequest.setCode(authCode); // 设置auth_code
oauthTokenRequest.setGrantType("authorization_code"); // 设置同意类型,值为authorization_code时,代表用code换取;值为refresh_token时,代表用refresh_token换取
AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(oauthTokenRequest); // 向支付宝发送请求并获得响应
// 判断是否换取到authToken
if(null != oauthTokenResponse && oauthTokenResponse.isSuccess()) { // 成功获得authToken
// 判断是否需要获取用户的详细信息
if(isNeedDetail) { // 需要获取用户的详细信息
// 利用authToken获取用户信息
AlipayUserInfoShareRequest userinfoShareRequest = new AlipayUserInfoShareRequest(); // 创建用户信息共享请求对象
AlipayUserInfoShareResponse userinfoShareResponse = alipayClient.execute(userinfoShareRequest, oauthTokenResponse.getAccessToken()); // 向支付宝发送请求并获得响应
// AlipayUserUserinfoShareRequest userinfoShareRequest = new AlipayUserUserinfoShareRequest();
// AlipayUserUserinfoShareResponse userinfoShareResponse = alipayClient.execute(userinfoShareRequest, oauthTokenResponse.getAccessToken());
// LOG.debug("======================用户真实姓名={}======================", userinfoShareResponse.getRealName());
// 判断是否能获得用户信息
if(null != userinfoShareResponse && userinfoShareResponse.isSuccess()) { // 成功获得用户信息
LOG.info("获取用户信息成功:{}", userinfoShareResponse.getBody()); // 获取用户信息成功,打日志
return userinfoShareResponse; // 返回响应对象,调用者可以用该对象获取用户的详细信息
} else { // 获取用户信息失败
LOG.error("获取用户信息失败"); // 获取用户信息失败,打日志
return null; // 返回空
}
} else { // 不需要获取用户的详细信息
LOG.info("获取用户openId成功:{}", oauthTokenResponse.getUserId()); // 获取用户openId成功,打日志
return oauthTokenResponse; // 返回响应对象,调用者可以用该对象获取用户的openId
}
} else { // 换取不到authToken
LOG.error("authCode换取authToken失败"); // authCode换取authToken失败,打日志
return null; // 返回空
}
} else { // 获取不到authCode
LOG.error("authCode获取失败"); // authCode获取失败,打日志
return null; // 返回空
}
} catch (AlipayApiException alipayApiException) {
LOG.error("获取oauthToken或用户信息失败"); // 获取oauthToken或用户信息失败,打日志
// 自行处理异常
alipayApiException.printStackTrace();
}
return null; // 返回空,正常时不会执行此语句
}
示例6
public Class<AlipayUserInfoShareResponse> getResponseClass() {
return AlipayUserInfoShareResponse.class;
}
示例7
public Class<AlipayUserInfoShareResponse> getResponseClass() {
return AlipayUserInfoShareResponse.class;
}
示例8
/**
* 用户确定授权后的获取用户详细信息;
*
* @param aliAccountInfo 封装了获取用户详细信息需要用到的数据的对象
* @param request http请求体
* @return AlipayUserInfoShareResponse实例,用于获取用户详细信息
* @throws IOException
*/
public static AlipayUserInfoShareResponse getUserDetailInfo(AliAccountInfo aliAccountInfo, HttpServerRequest request){
return (AlipayUserInfoShareResponse) getUserInfo(aliAccountInfo, true, request); // 调用上面方法
}