com.alipay.api.response.AlipaySystemOauthTokenResponse Java Examples

The following examples show how to use com.alipay.api.response.AlipaySystemOauthTokenResponse. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AuthAlipayRequest.java    From JustAuth with MIT License 6 votes vote down vote up
@Override
protected AuthToken getAccessToken(AuthCallback authCallback) {
    AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
    request.setGrantType("authorization_code");
    request.setCode(authCallback.getAuth_code());
    AlipaySystemOauthTokenResponse response = null;
    try {
        response = this.alipayClient.execute(request);
    } catch (Exception e) {
        throw new AuthException(e);
    }
    if (!response.isSuccess()) {
        throw new AuthException(response.getSubMsg());
    }
    return AuthToken.builder()
        .accessToken(response.getAccessToken())
        .uid(response.getUserId())
        .expireIn(Integer.parseInt(response.getExpiresIn()))
        .refreshToken(response.getRefreshToken())
        .build();
}
 
Example #2
Source File: AlipayOAuth2Template.java    From cola with MIT License 6 votes vote down vote up
@Override
protected AccessGrant postForAccessGrant(String accessTokenUrl, MultiValueMap<String, String> parameters) {

	AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", properties.getAppId(), properties.getPrivateKey(), properties.getPrivateKey(), properties.getCharset(), properties.getPublicKey(), properties.getSignType());
	AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
	request.setCode(parameters.getFirst("credential"));
	request.setGrantType("authorization_code");
	try {
		AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
		if (oauthTokenResponse.isSuccess()) {
			return new AccessGrant(oauthTokenResponse.getAccessToken(), null, oauthTokenResponse.getRefreshToken(), Long.valueOf(oauthTokenResponse.getExpiresIn()));
		}else{
			throw new IllegalArgumentException(oauthTokenResponse.getCode() + ":" + oauthTokenResponse.getMsg());
		}
	} catch (AlipayApiException e) {
		//处理异常
		throw new IllegalArgumentException(e.getMessage());
	}

}
 
Example #3
Source File: AlipayOauthSubRouter.java    From AlipayWechatPlatform with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 支付宝静默授权的回调方法
 * 由支付宝服务器调用
 *
 * @param rc Vertx的RoutingContext对象
 * @author Leibniz.Hu
 */
private void oauthBaseCallback(RoutingContext rc) {
    HttpServerRequest req = rc.request();
    HttpServerResponse resp = rc.response();
    Integer eid = Integer.parseInt(req.getParam("eid"));
    getAccountAndExecute(resp, eid, aliAcc -> {
        AlipaySystemOauthTokenResponse oauthRes = AliPayApi.getUserId(aliAcc, req);
        oauthSuccessProcess(req, resp, oauthRes, url -> log.info("授权成功,OpenID={},{},准备跳转到{}", oauthRes.getUserId(), oauthRes.getAlipayUserId(), url));
    });
}
 
Example #4
Source File: AlipaySystemOauthTokenRequest.java    From alipay-sdk-java-all with Apache License 2.0 4 votes vote down vote up
public Class<AlipaySystemOauthTokenResponse> getResponseClass() {
	return AlipaySystemOauthTokenResponse.class;
}
 
Example #5
Source File: AliPayApi.java    From AlipayWechatPlatform with GNU General Public License v3.0 4 votes vote down vote up
/**
     * 该方法是用户确定授权后的获取用户信息的方法;
     * 方法先解析请求参数,获取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; // 返回空,正常时不会执行此语句
    }
 
Example #6
Source File: AlipaySystemOauthTokenRequest.java    From alipay-sdk with Apache License 2.0 4 votes vote down vote up
public Class<AlipaySystemOauthTokenResponse> getResponseClass() {
	return AlipaySystemOauthTokenResponse.class;
}
 
Example #7
Source File: AlipaySystemOauthTokenRequest.java    From pay with Apache License 2.0 4 votes vote down vote up
public Class<AlipaySystemOauthTokenResponse> getResponseClass() {
	return AlipaySystemOauthTokenResponse.class;
}
 
Example #8
Source File: AliPayApi.java    From AlipayWechatPlatform with GNU General Public License v3.0 2 votes vote down vote up
/**
 * 用户确定授权后的获取用户简要信息,主要是OpenID;
 *
 * @param aliAccountInfo 封装了获取用户详细信息需要用到的数据的对象
 * @param request http请求体
 * @return AlipaySystemOauthTokenResponse实例,用于获取用于简要信息
 * @throws IOException
 */
public static AlipaySystemOauthTokenResponse getUserId(AliAccountInfo aliAccountInfo, HttpServerRequest request){
    return (AlipaySystemOauthTokenResponse) getUserInfo(aliAccountInfo, false, request); // 调用上面方法
}