reCAPTCHA Enterprise 사이트 키 나열

지정된 프로젝트 ID에 대해 제공된 모든 키를 나열합니다.

코드 샘플

Java

reCAPTCHA에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


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

reCAPTCHA에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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

reCAPTCHA에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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

reCAPTCHA에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

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

다음 단계

다른 Google Cloud 제품의 코드 샘플을 검색하고 필터링하려면 Google Cloud 샘플 브라우저를 참조하세요.