com.google.api.ads.adwords.extension.ratelimiter.AdWordsServicesWithRateLimiter Java Examples

The following examples show how to use com.google.api.ads.adwords.extension.ratelimiter.AdWordsServicesWithRateLimiter. 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: AdWordsApiUtil.java    From keyword-optimizer with Apache License 2.0 5 votes vote down vote up
/**
 * Initializes this utility (credentials, sessions, ...).
 * 
 * @throws OAuthException in case of an authentication error
 * @throws ValidationException in case there is no refresh token
 * @throws ConfigurationLoadException in case of an error loading the config file
 */
private void init() throws OAuthException, ValidationException, ConfigurationLoadException {
  logger.info("Initializing session and services interface");
  
  try {
    // Generate a refreshable OAuth2 credential similar to a ClientLogin token
    // and can be used in place of a service account.
    Credential oAuth2Credential = new OfflineCredentials.Builder()
            .forApi(Api.ADWORDS)
            .fromFile(configPath)
            .build()
            .generateCredential();
    
    // Construct an AdWordsSession.
    String userAgentFromConfig = getUserAgentFromConfig();
    session = new AdWordsSession.Builder()
        .fromFile(configPath)
        .withUserAgent(PREFIX_USER_AGENT + userAgentFromConfig)
        .withOAuth2Credential(oAuth2Credential)
        .build();

    // Create the services interface object.
    services = new AdWordsServicesWithRateLimiter(AdWordsServices.getInstance());
  } catch (ValidationException e) {
    if ("refreshToken".equalsIgnoreCase(e.getTrigger())) {
      retrieveRefreshToken();
    } else {
      logger.error("General exception", e);
    }

    throw new OAuthException("Please add your refreshToken to your ads.properties file", e);
  }
}