2

Unable to validate google recaptcha enterprise. getting error:

java.io.IOException: The Application Default Credentials are not available.
java.io.IOException: The Application Default Credentials are not available. They are available if running in Google Compute Engine. Otherwise, the environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined pointing to a file defining the credentials. 

I have also created credential json with service account and set in environment variable GOOGLE_APPLICATION_CREDENTIALS and alternatively created credential json with aws external account and set in environment variable. but "I was getting required parameters" must be specified error Note: I am able to get the token from client side but this error is from server side.

Code blocks used to get credentials: Method1 :

 // If you don't specify credentials when constructing the client, the client library will
 // look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
 Storage storage = StorageOptions.getDefaultInstance().getService();
 System.out.println("Buckets:");
 Page<Bucket> buckets = storage.list();
for (Bucket bucket : buckets.iterateAll()) {
 System.out.println(bucket.toString());
 }

Method2:

System.setProperty("GOOGLE_APPLICATION_CREDENTIALS", jsonPath);
System.out.println("GOOGLE_APPLICATION_CREDENTIALS");
System.out.println(System.getProperty("GOOGLE_APPLICATION_CREDENTIALS"));
FileInputStream fileInputStream = new FileInputStream(jsonPath);
 GoogleCredentials credentials = GoogleCredentials.fromStream(fileInputStream)
 .createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
 Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
 System.out.println("Buckets:");
 Page<Bucket> buckets = storage.list();
 for (Bucket bucket : buckets.iterateAll()) {
 System.out.println(bucket.toString());
 }

Interpreting assesment:

/**
 * Create an assessment to analyze the risk of an UI action.
 *
 * @param projectID: GCloud Project ID
 * @param recaptchaSiteKey: Site key obtained by registering a domain/app to use recaptcha services.
 * @param token: The token obtained from the client on passing the recaptchaSiteKey.
 * @param recaptchaAction: Action name corresponding to the token.
 */
 public static void createAssessment(String projectID, String recaptchaSiteKey, String token,
 String recaptchaAction)
 throws IOException {
 // Initialize a client that will be used to send requests. This client needs to be created only
 // once, and can be reused for multiple requests. After completing all of your requests, call
 // the `client.close()` method on the client to safely
 // clean up any remaining background resources.
 try (RecaptchaEnterpriseServiceClient client = RecaptchaEnterpriseServiceClient.create()) {
 // Specify a name for this assessment.
 String assessmentName = "assessment-name";
 // Set the properties of the event to be tracked.
 Event event = Event.newBuilder()
 .setSiteKey(recaptchaSiteKey)
 .setToken(token)
 .build();
 // Build the assessment request.
 CreateAssessmentRequest createAssessmentRequest = CreateAssessmentRequest.newBuilder()
 .setParent(ProjectName.of(projectID).toString())
 .setAssessment(Assessment.newBuilder().setEvent(event).setName(assessmentName).build())
 .build();
 Assessment response = client.createAssessment(createAssessmentRequest);
 // Check if the token is valid.
 if (!response.getTokenProperties().getValid()) {
 System.out.println("The CreateAssessment call failed because the token was: " +
 response.getTokenProperties().getInvalidReason().name());
 return;
 }
 // Check if the expected action was executed.
 if (!response.getTokenProperties().getAction().equals(recaptchaAction)) {
 System.out.println("The action attribute in your reCAPTCHA tag " +
 "does not match the action you are expecting to score");
 return;
 }
 // Get the risk score and the reason(s).
 // For more information on interpreting the assessment,
 // see: https://cloud.google.com/recaptcha-enterprise/docs/interpret-assessment
 float recaptchaScore = response.getRiskAnalysis().getScore();
 System.out.println("The reCAPTCHA score is: " + recaptchaScore);
 for (ClassificationReason reason : response.getRiskAnalysis().getReasonsList()) {
 System.out.println(reason);
 }
 }
11
  • An error message without details is not helpful. Also, try to format your text to make it easier to read. Commented Sep 19, 2021 at 7:48
  • Are you developing your app on an IDE? If so, please make sure that GOOGLE_APPLICATION_CREDENTIALS is configured within the IDE. Commented Sep 20, 2021 at 6:12
  • @Dondi am getting the value of GOOGLE_APPLICATION_CREDENTIALS from env variable. StorageOptions.getDefaultInstance().getService(); This is the code I am using to get the credentials implicitly. I even tried to call explicitly with GoogleCredentials.fromStream(new FileInputStream(jsonPath)) .createScoped(Lists.newArrayList("googleapis.com/auth/cloud-platform")); Commented Sep 20, 2021 at 10:01
  • Added more details in the description Commented Sep 20, 2021 at 10:06
  • I think you posted a different snippet. Originally your tag is Recaptcha Enterprise but the newer sample uses Cloud Storage. You might want to show how you initialize the Recaptcha service client instead. Commented Sep 21, 2021 at 0:52

2 Answers 2

1

I was able to acheive the same using recaptcha V3 instead of enterprise version. As we had to acheive scored based assesment , V3 supports score based validation.

answered Sep 30, 2021 at 15:39
Sign up to request clarification or add additional context in comments.

Comments

0

I was able to solve this issue by:

  1. using a valid google credentials file, which was generated at the google enterprise portal with the correct permissions associated with it, and in the following format

    { "type": "", "project_id": "", "private_key_id": "", "private_key": "", "client_email": "", "client_id": "", "auth_uri": "", "token_uri": "", "auth_provider_x509_cert_url": "", "client_x509_cert_url": "", "universe_domain": "" }

  2. either using default recaptcha enterprise java's library feature to read this file from SO's environment variable 'GOOGLE_APPLICATION_CREDENTIALS', which must contain file's absolute path, or if you don't wanna or can't use it this way, by using an alternative

    GoogleCredentials credentials;
    try (ByteArrayInputStream credentialsStream = new ByteArrayInputStream([file content as string or new FileInputStream("")])) {
     credentials = GoogleCredentials.fromStream(credentialsStream);
    }
    final RecaptchaEnterpriseServiceSettings settings = RecaptchaEnterpriseServiceSettings.newBuilder()
     .setCredentialsProvider(() -> credentials)
     .build();
    return RecaptchaEnterpriseServiceClient.create(settings);
    
answered Nov 11, 2024 at 12:28

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.