use of com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest 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();
}
use of com.hyq0719.mktapi.tencent.bean.oauth.OauthTokenRequest 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();
}
Aggregations