Enumerar claves de sitios de reCAPTCHA Enterprise

Enumera todas las claves presentes para el ID del proyecto dado.

Muestra de código

Java

Para autenticarte en reCAPTCHA Enterprise, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.


import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.ListKeysPagedResponse;
import com.google.recaptchaenterprise.v1.Key;
import com.google.recaptchaenterprise.v1.ListKeysRequest;
import com.google.recaptchaenterprise.v1.ProjectName;
import java.io.IOException;

public class ListSiteKeys {

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

    listSiteKeys(projectID);
  }

  /**
   * List all keys present under the given project ID.
   *
   * @param projectID : GCloud Project ID.
   */
  public static ListKeysPagedResponse listSiteKeys(String projectID) 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()) {
      // 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;
    }
  }
}

PHP

Para autenticarte en reCAPTCHA Enterprise, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

use Google\ApiCore\ApiException;
use Google\Cloud\RecaptchaEnterprise\V1\Client\RecaptchaEnterpriseServiceClient;
use Google\Cloud\RecaptchaEnterprise\V1\ListKeysRequest;

/**
 * List all the reCAPTCHA keys associate to a Google Cloud project
 *
 * @param string $projectId Your Google Cloud project ID
 */
function list_keys(string $projectId): void
{
    $client = new RecaptchaEnterpriseServiceClient();
    $formattedProject = $client->projectName($projectId);

    try {
        $listKeysRequest = (new ListKeysRequest())
            ->setParent($formattedProject)
            ->setPageSize(2);
        $response = $client->listKeys($listKeysRequest);

        print('Keys fetched' . PHP_EOL);

        // Either iterate over all the keys and let the library handle the paging
        foreach ($response->iterateAllElements() as $key) {
            print($key->getDisplayName() . PHP_EOL);
        }

        // Or fetch each page and process the keys as needed
        // foreach ($response->iteratePages() as $page) {
        //     foreach ($page as $key) {
        //         print($key->getDisplayName() . PHP_EOL);
        //     }
        // }
    } catch (ApiException $e) {
        print('listKeys() call failed with the following error: ');
        print($e);
    }
}

Python

Para autenticarte en reCAPTCHA Enterprise, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

from google.cloud import recaptchaenterprise_v1
from google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.pagers import (
    ListKeysPager,
)

def list_site_keys(project_id: str) -> ListKeysPager:
    """List all keys present under the given project ID.

    Args:
    project_id: GCloud Project ID.
    """

    project_name = f"projects/{project_id}"

    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    # Set the project id to list the keys present in it.
    request = recaptchaenterprise_v1.ListKeysRequest()
    request.parent = project_name

    response = client.list_keys(request)
    print("Listing reCAPTCHA site keys: ")
    for i, key in enumerate(response):
        print(f"{str(i)}. {key.name}")

    return response

Ruby

Para autenticarte en reCAPTCHA Enterprise, configura las credenciales predeterminadas de la aplicación. Si deseas obtener más información, consulta Configura la autenticación para un entorno de desarrollo local.

require "google/cloud/recaptcha_enterprise"

# List all site keys registered to use recaptcha services.
#
# @param project_id [String] GCloud Project ID.
# @return [void]
def list_site_keys project_id:
  # Create the reCAPTCHA client.
  client = ::Google::Cloud::RecaptchaEnterprise.recaptcha_enterprise_service

  response = client.list_keys parent: "projects/#{project_id}"
  puts "Listing reCAPTCHA site keys: "
  response.each do |key|
    puts key.name
  end
end

¿Qué sigue?

Para buscar y filtrar muestras de código para otros productos de Google Cloud, consulta el navegador de muestra de Google Cloud.