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();
}
Aggregations