com.alipay.api.request.AlipaySystemOauthTokenRequest Java Examples

The following examples show how to use com.alipay.api.request.AlipaySystemOauthTokenRequest. 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());
	}

}