Search in sources :

Example 1 with Oauth2TokenRequest

use of com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenRequest in project marketing-api-java-sdks by Hyq0719.

the class VivoAuthorizer method authorize.

@Override
public Result<AuthToken> authorize(String advertiserId, String authCode) {
    VivoProperties.VivoOAuth2Config config = vivoProperties.getConfigs().get(advertiserId);
    if (config == null) {
        return Result.ofFail();
    }
    if (isDebugMode()) {
        return Util.authorizeForTest(advertiserId, authCode, channel());
    }
    Oauth2TokenRequest request = new Oauth2TokenRequest();
    request.clientId(Integer.valueOf(config.getClientId())).clientSecret(config.getSecret()).grantType("code").code(authCode);
    try {
        VivoResponse<Oauth2TokenResponseData> response = vivoSdkService.getTokenApi().oauth2Token().execute(request);
        if (!response.isSuccessful()) {
            return Result.ofFail();
        }
        Oauth2TokenResponseData data = response.getData();
        AuthToken authToken = new AuthToken();
        authToken.setChannel(channel());
        authToken.setAdvertiserId(advertiserId);
        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 : Oauth2TokenResponseData(com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenResponseData) Oauth2TokenRequest(com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenRequest) AuthToken(test.auth.bean.AuthToken) ApiException(com.hyq0719.mktapi.common.exception.ApiException)

Aggregations

ApiException (com.hyq0719.mktapi.common.exception.ApiException)1 Oauth2TokenRequest (com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenRequest)1 Oauth2TokenResponseData (com.hyq0719.mktapi.vivo.bean.token.Oauth2TokenResponseData)1 AuthToken (test.auth.bean.AuthToken)1