サイトキーを移行する

サイトキーを reCAPTCHA(Enterprise 以外)から reCAPTCHA Enterprise に移行する

コードサンプル

Java


import com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient;
import com.google.recaptchaenterprise.v1.Key;
import com.google.recaptchaenterprise.v1.KeyName;
import com.google.recaptchaenterprise.v1.MigrateKeyRequest;
import java.io.IOException;

public class MigrateKey {

  public static void main(String[] args) throws IOException {
    String projectId = "project-id";
    String recaptchaSiteKey = "recaptcha-site-key";

    migrateKey(projectId, recaptchaSiteKey);
  }

  /**
   * Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key
   * using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate
   * to reCAPTCHA Enterprise. For more info, see:
   * https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
   *
   * @param projectId: Google Cloud Project Id.
   * @param recaptchaSiteKey: Specify the site key to migrate.
   */
  public static void migrateKey(String projectId, String recaptchaSiteKey) 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()) {

      // Specify the key name to migrate.
      MigrateKeyRequest migrateKeyRequest =
          MigrateKeyRequest.newBuilder()
              .setName(KeyName.of(projectId, recaptchaSiteKey).toString())
              .build();

      Key response = client.migrateKey(migrateKeyRequest);

      // To verify if the site key has been migrated, use 'ListSiteKeys' and check if the
      // key is present.
      for (Key key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {
        if (key.equals(response)) {
          System.out.printf("Key migrated successfully: %s", recaptchaSiteKey);
        }
      }
    }
  }
}

Python

from google.cloud import recaptchaenterprise_v1

from list_site_keys import list_site_keys

def migrate_site_key(project_id: str, recaptcha_site_key: str) -> None:
    """ Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.
        If you created the key using Admin console: https://www.google.com/recaptcha/admin/site,
        then use this API to migrate to reCAPTCHA Enterprise.
        For more info, see: https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha
    Args:
    project_id: Google Cloud Project ID.
    recaptcha_site_key: Specify the site key to migrate.
    """

    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    # Specify the key name to migrate.
    name = f"projects/{project_id}/keys/{recaptcha_site_key}"
    request = recaptchaenterprise_v1.MigrateKeyRequest()
    request.name = name

    response = client.migrate_key(request)
    # To verify if the site key has been migrated, use 'list_site_keys' to check if the
    # key is present.
    for key in list_site_keys(project_id):
        if key.name == response.name:
            print(f"Key migrated successfully: {recaptcha_site_key}")

Ruby

require "google/cloud/recaptcha_enterprise"

# Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.
#
# @param project_id [String] GCloud Project ID.
# @param site_key [String] Site key to be updated.
# @return [void]
def migrate_site_key project_id:, site_key:
  # Create the reCAPTCHA client.
  client = ::Google::Cloud::RecaptchaEnterprise.recaptcha_enterprise_service

  response = client.migrate_key name: "projects/#{project_id}/keys/#{site_key}"

  # To verify if the site key has been migrated, use 'list_keys' to check if the
  # key is present.
  keys = client.list_keys parent: "projects/#{project_id}"
  keys.each do |key|
    puts "Key migrated successfully: #{site_key}" if key.name == response.name
  end
end

次のステップ

他の Google Cloud プロダクトに関連するコードサンプルの検索およびフィルタ検索を行うには、Google Cloud のサンプルをご覧ください。