Search in sources :

Example 1 with OauthTokenResponseData

use of com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData in project marketing-api-java-sdks by Hyq0719.

the class TencentAuthorizer method refresh.

@Override
public Result<AuthToken> refresh(AuthToken authToken) {
    TencentProperties.TencentOAuth2Config config = tencentProperties.getConfigs().get(authToken.getAdvertiserId());
    if (config == null) {
        return Result.ofFail();
    }
    if (isDebugMode()) {
        return Util.refreshForTest(authToken);
    }
    OauthTokenRequest request = new OauthTokenRequest();
    request.clientId(Integer.valueOf(config.getClientId())).clientSecret(config.getSecret()).grantType("refresh_token").refreshToken(authToken.getRefreshToken());
    try {
        TencentResponse<OauthTokenResponseData> response = tencentSdkService.getOauthApi().oauthToken().execute(request);
        if (!response.isSuccessful()) {
            return Result.ofFail();
        }
        OauthTokenResponseData data = response.getData();
        AuthToken newAuthToken = authToken.newToken(data.getAccessToken(), data.getRefreshToken());
        return Result.ofSuccessful(newAuthToken);
    } catch (ApiException e) {
        e.printStackTrace();
    }
    return Result.ofFail();
}
Also used : OauthTokenResponseData(com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData) AuthToken(test.auth.bean.AuthToken) OauthTokenRequest(com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Example 2 with OauthTokenResponseData

use of com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData in project marketing-api-java-sdks by Hyq0719.

the class TencentAuthorizer method authorize.

@Override
public Result<AuthToken> authorize(String advertiserId, String authCode) {
    TencentProperties.TencentOAuth2Config config = tencentProperties.getConfigs().get(advertiserId);
    if (config == null) {
        return Result.ofFail();
    }
    if (isDebugMode()) {
        return Util.authorizeForTest(advertiserId, authCode, channel());
    }
    OauthTokenRequest request = new OauthTokenRequest();
    request.clientId(Integer.valueOf(config.getClientId())).clientSecret(config.getSecret()).grantType("authorization_code").authorizationCode(authCode).redirectUri(config.getCallbackUrl());
    try {
        TencentResponse<OauthTokenResponseData> response = tencentSdkService.getOauthApi().oauthToken().execute(request);
        if (!response.isSuccessful()) {
            return Result.ofFail();
        }
        OauthTokenResponseData data = response.getData();
        AuthToken authToken = new AuthToken();
        authToken.setChannel(channel());
        authToken.setAdvertiserId(data.getAuthorizerInfo().getWechatAccountId());
        authToken.setAccessToken(data.getAccessToken());
        authToken.setRefreshToken(data.getRefreshToken());
        long now = System.currentTimeMillis();
        authToken.setCreateTime(now);
        authToken.setRefreshTime(now);
        return Result.ofSuccessful(authToken);
    } catch (ApiException e) {
        e.printStackTrace();
    }
    return Result.ofFail();
}
Also used : OauthTokenResponseData(com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData) AuthToken(test.auth.bean.AuthToken) OauthTokenRequest(com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Aggregations

ApiException (com.hyq0719.mktapi.common.exception.ApiException)2 OauthTokenRequest (com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest)2 OauthTokenResponseData (com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenResponseData)2 AuthToken (test.auth.bean.AuthToken)2