com.google.api.ads.common.lib.auth.GoogleClientSecretsBuilder Java Examples

The following examples show how to use com.google.api.ads.common.lib.auth.GoogleClientSecretsBuilder. 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: InstalledOAuth2Authenticator.java    From adwords-alerting with Apache License 2.0 5 votes vote down vote up
private GoogleAuthorizationCodeFlow getAuthorizationFlow() {
  GoogleClientSecrets clientSecrets = null;
  try {
    clientSecrets =
        new GoogleClientSecretsBuilder()
            .forApi(Api.ADWORDS)
            .withClientSecrets(clientId, clientSecret)
            .build();
  } catch (ValidationException e) {
    System.err.println(
        "Please input your client ID and secret into your properties file, which is either "
        + "located in your home directory in your java/resources directory, or on your "
        + "classpath. If you do not have a client ID or secret, please create one in the "
        + "API console: https://code.google.com/apis/console#access");
    System.exit(1);
  }

  return new GoogleAuthorizationCodeFlow
      .Builder(
          new NetHttpTransport(),
          new JacksonFactory(),
          clientSecrets,
          Lists.newArrayList(SCOPE))
      // Set the access type to offline so that the token can be refreshed.
      // By default, the library will automatically refresh tokens when it
      // can, but this can be turned off by setting
      // api.adwords.refreshOAuth2Token=false in your ads.properties file.
      .setAccessType("offline")
      .build();
}
 
Example #2
Source File: GetRefreshToken.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  // Get the client ID and secret from the ads.properties file.
  // If you do not have a client ID or secret, please create one in the
  // API console: https://console.developers.google.com/project and set it
  // in the ads.properties file.
  GoogleClientSecrets clientSecrets = null;
  try {
    clientSecrets = new GoogleClientSecretsBuilder()
        .forApi(Api.AD_MANAGER)
        .fromFile()
        .build();
  } catch (ValidationException e) {
    System.err.println(
        "Please input your client ID and secret into your ads.properties file, which is either "
        + "located in your home directory in your src/main/resources directory, or "
        + "on your classpath. If you do not have a client ID or secret, please create one in "
        + "the API console: https://console.developers.google.com/project");
    System.exit(1);
  }

  // Get the OAuth2 credential.
  Credential credential = getOAuth2Credential(clientSecrets);

  System.out.printf("Your refresh token is: %s%n", credential.getRefreshToken());

  // Enter the refresh token into your ads.properties file.
  System.out.printf("In your ads.properties file, modify:%n%napi.admanager.refreshToken=%s%n",
      credential.getRefreshToken());
}
 
Example #3
Source File: GetRefreshTokenWithoutPropertiesFile.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
  BufferedReader reader = new BufferedReader(new InputStreamReader(System.in, UTF_8));
  System.out.println("Please input your client ID and secret. "
        + "If you do not have a client ID or secret, please create one in "
        + "the API console: https://console.developers.google.com");
  System.out.println("Enter your client ID: ");
  String clientId = reader.readLine();
  if (Strings.isNullOrEmpty(clientId)) {
    System.err.println("Please input your client ID.");
    System.exit(1);
  }
  System.out.println("Enter your client secret: ");
  String clientSecret = reader.readLine();
  if (Strings.isNullOrEmpty(clientSecret)) {
    System.err.println("Please input your client secret.");
    System.exit(1);
  }

  GoogleClientSecrets clientSecrets = null;
  try {
    clientSecrets = new GoogleClientSecretsBuilder()
        .forApi(Api.AD_MANAGER)
        .withClientSecrets(clientId, clientSecret)
        .build();
  } catch (ValidationException e) {
    System.err.println(
        "Please input your client ID and secret. If you do not have a "
        + "client ID or secret, please create one in "
        + "the API console: https://console.developers.google.com");
    System.exit(1);
  }

  // Get the OAuth2 credential.
  Credential credential = getOAuth2Credential(clientSecrets);

  System.out.printf("Your refresh token is: %s%n", credential.getRefreshToken());
}
 
Example #4
Source File: GetRefreshToken.java    From googleads-java-lib with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
  // Get the client ID and secret from the ads.properties file.
  // If you do not have a client ID or secret, please create one in the
  // API console: https://console.developers.google.com/project and set it
  // in the ads.properties file.
  GoogleClientSecrets clientSecrets = null;
  try {
    clientSecrets = new GoogleClientSecretsBuilder()
        .forApi(Api.ADWORDS)
        .fromFile()
        .build();
  } catch (ValidationException e) {
    System.err.println(
        "Please input your client ID and secret into your ads.properties file, which is either "
        + "located in your home directory, in your src/main/resources directory, or "
        + "on your classpath. If you do not have a client ID or secret, please create one in "
        + "the API console: https://console.developers.google.com/project");
    return;
  } catch (ConfigurationLoadException cle) {
    System.err.printf(
        "Failed to load configuration from the %s file. Exception: %s%n",
        DEFAULT_CONFIGURATION_FILENAME, cle);
    return;
  }

  // Get the OAuth2 credential.
  Credential credential = null;
  try {
    credential = getOAuth2Credential(clientSecrets);
  } catch (IOException ioe) {
    System.err.printf("Failed to generate credentials. Exception: %s%n", ioe);
    return;
  }

  System.out.printf("Your refresh token is: %s%n", credential.getRefreshToken());

  // Enter the refresh token into your ads.properties file.
  System.out.printf("In your ads.properties file, modify:%n%napi.adwords.refreshToken=%s%n", 
      credential.getRefreshToken());
}