Membuat anotasi penilaian

Memberikan anotasi pada penilaian untuk memberikan masukan tentang kebenaran analisis yang dilakukan oleh reCAPTCHA Enterprise.

Contoh kode

Java

Untuk melakukan autentikasi ke reCAPTCHA, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.


import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.recaptchaenterprise.v1.AnnotateAssessmentRequest;
import com.google.recaptchaenterprise.v1.AnnotateAssessmentRequest.Annotation;
import com.google.recaptchaenterprise.v1.AnnotateAssessmentRequest.Reason;
import com.google.recaptchaenterprise.v1.AnnotateAssessmentResponse;
import com.google.recaptchaenterprise.v1.AssessmentName;
import java.io.IOException;

public class AnnotateAssessment {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectID = "project-id";
    String assessmentId = "assessment-id";
    annotateAssessment(projectID, assessmentId);
  }

  /**
   * Pre-requisite: Create an assessment before annotating.
   *
   * <p>Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
   *
   * @param projectID: GCloud Project id
   * @param assessmentId: Value of the 'name' field returned from the CreateAssessment call.
   */
  public static void annotateAssessment(String projectID, String assessmentId) throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // 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()) {
      // Build the annotation request.
      // For more info on when/how to annotate, see:
      // https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
      AnnotateAssessmentRequest annotateAssessmentRequest =
          AnnotateAssessmentRequest.newBuilder()
              .setName(AssessmentName.of(projectID, assessmentId).toString())
              .setAnnotation(Annotation.FRAUDULENT)
              .addReasons(Reason.FAILED_TWO_FACTOR)
              .build();

      // Empty response is sent back.
      AnnotateAssessmentResponse response = client.annotateAssessment(annotateAssessmentRequest);
      System.out.println("Annotated response sent successfully ! " + response);
    }
  }
}

Python

Untuk melakukan autentikasi ke reCAPTCHA, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, lihat Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

from google.cloud import recaptchaenterprise_v1


def annotate_assessment(project_id: str, assessment_id: str) -> None:
    """Pre-requisite: Create an assessment before annotating.
        Annotate an assessment to provide feedback on the correctness of recaptcha prediction.
    Args:
        project_id: Google Cloud Project ID
        assessment_id: Value of the 'name' field returned from the create_assessment() call.
    """

    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    assessment_name = f"projects/{project_id}/assessments/{assessment_id}"
    # Build the annotation request.
    # For more info on when/how to annotate, see:
    # https://cloud.google.com/recaptcha-enterprise/docs/annotate-assessment#when_to_annotate
    request = recaptchaenterprise_v1.AnnotateAssessmentRequest()
    request.name = assessment_name
    request.annotation = request.Annotation.FRAUDULENT
    request.reasons = [request.Reason.FAILED_TWO_FACTOR]

    # Empty response is sent back.
    client.annotate_assessment(request)
    print("Annotated response sent successfully ! ")

Langkah selanjutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat browser contoh Google Cloud.