use of com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient in project java-recaptchaenterprise by googleapis.
the class CreateSiteKey method createSiteKey.
/**
* Create reCAPTCHA Site key which binds a domain name to a unique key.
*
* @param projectID : GCloud Project ID.
* @param domainName : Specify the domain name in which the reCAPTCHA should be activated.
*/
public static String createSiteKey(String projectID, String domainName) throws IOException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the type of reCAPTCHA to be displayed.
// For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
Key scoreKey = Key.newBuilder().setDisplayName("any_descriptive_name_for_the_key").setWebSettings(WebKeySettings.newBuilder().addAllowedDomains(domainName).setAllowAmpTraffic(false).setIntegrationType(IntegrationType.SCORE).build()).build();
CreateKeyRequest createKeyRequest = CreateKeyRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).setKey(scoreKey).build();
// Get the name of the created reCAPTCHA site key.
Key response = client.createKey(createKeyRequest);
String keyName = response.getName();
String recaptchaSiteKey = keyName.substring(keyName.lastIndexOf("/") + 1);
System.out.println("reCAPTCHA Site key created successfully. Site Key: " + recaptchaSiteKey);
return recaptchaSiteKey;
}
}
use of com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient in project java-recaptchaenterprise by googleapis.
the class DeleteSiteKey method deleteSiteKey.
/**
* Delete the given reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to be deleted.
*/
public static void deleteSiteKey(String projectID, String recaptchaSiteKey) throws IOException, ExecutionException, InterruptedException, TimeoutException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the project ID and reCAPTCHA site key.
DeleteKeyRequest deleteKeyRequest = DeleteKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKey).toString()).build();
client.deleteKeyCallable().futureCall(deleteKeyRequest).get(5, TimeUnit.SECONDS);
System.out.println("reCAPTCHA Site key successfully deleted !");
}
}
use of com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient in project java-recaptchaenterprise by googleapis.
the class GetSiteKey method getSiteKey.
/**
* Get the reCAPTCHA site key present under the project ID.
*
* @param projectID: GCloud Project ID.
* @param recaptchaSiteKey: Specify the site key to get the details.
*/
public static void getSiteKey(String projectID, String recaptchaSiteKey) throws IOException, InterruptedException, ExecutionException, TimeoutException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Construct the "GetSiteKey" request.
GetKeyRequest getKeyRequest = GetKeyRequest.newBuilder().setName(KeyName.of(projectID, recaptchaSiteKey).toString()).build();
// Wait for the operation to complete.
ApiFuture<Key> futureCall = client.getKeyCallable().futureCall(getKeyRequest);
Key key = futureCall.get(5, TimeUnit.SECONDS);
System.out.println("Successfully obtained the key !" + key.getName());
}
}
use of com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient in project java-recaptchaenterprise by googleapis.
the class ListSiteKeys method listSiteKeys.
/**
* List all keys present under the given project ID.
*
* @param projectID : GCloud Project ID.
*/
public static ListKeysPagedResponse listSiteKeys(String projectID) throws IOException {
// clean up any remaining background resources.
try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
// Set the project ID to list the keys present in it.
ListKeysRequest listKeysRequest = ListKeysRequest.newBuilder().setParent(ProjectName.of(projectID).toString()).build();
ListKeysPagedResponse response = client.listKeys(listKeysRequest);
System.out.println("Listing reCAPTCHA site keys: ");
for (Key key : response.iterateAll()) {
System.out.println(key.getName());
}
return response;
}
}
Aggregations