【问题标题】:Unable to validate google recaptcha enterprise. getting error: java.io.IOException: The Application Default Credentials are not available无法验证 google recaptcha 企业。出现错误:java.io.IOException:应用程序默认凭据不可用
【发布时间】:2021-09-19 05:16:34
【问题描述】:

无法验证 google recaptcha 企业。出现错误:

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. 

我还使用服务帐户创建了凭证 json 并设置在环境变量 GOOGLE_APPLICATION_CREDENTIALS 中,或者使用 aws 外部帐户创建了凭证 json 并设置在环境变量中。但是“我正在获取所需的参数”必须指定错误 注意:我可以从客户端获取令牌,但此错误来自服务器端。

用于获取凭据的代码块: 方法一:

  // 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());
  }

方法二:

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());
  }

解读评估:

/**
      * 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);
         }
       }

【问题讨论】:

  • 没有详细信息的错误消息没有帮助。另外,请尝试格式化文本以使其更易于阅读。
  • 您是否在 IDE 上开发您的应用程序?如果是这样,请确保在 IDE 中配置了 GOOGLE_APPLICATION_CREDENTIALS
  • @Dondi 正在从 env 变量中获取 GOOGLE_APPLICATION_CREDENTIALS 的值。 StorageOptions.getDefaultInstance().getService();这是我用来隐式获取凭据的代码。我什至尝试使用 GoogleCredentials.fromStream(new FileInputStream(jsonPath)) .createScoped(Lists.newArrayList("googleapis.com/auth/cloud-platform"));
  • 在描述中添加了更多细节
  • 我认为您发布了不同的 sn-p。最初您的标签是 Recaptcha Enterprise,但较新的示例使用 Cloud Storage。您可能想展示如何初始化 Recaptcha 服务客户端。

标签: java google-cloud-platform recaptcha recaptcha-enterprise


【解决方案1】:

我能够使用recaptcha V3 而不是企业版来实现相同的目标。由于我们必须实现基于分数的评估,V3 支持基于分数的验证。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-23
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 2018-02-18
    相关资源
    最近更新 更多