Migrer une clé de site

Migrez une clé de site de reCAPTCHA (version autre qu'Enterprise) vers reCAPTCHA Enterprise.

Exemple de code

Java

Pour vous authentifier auprès de reCAPTCHA, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.


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

Pour vous authentifier auprès de reCAPTCHA, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

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

Pour vous authentifier auprès de reCAPTCHA, configurez les Identifiants par défaut de l'application. Pour en savoir plus, consultez Configurer l'authentification pour un environnement de développement local.

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

Étape suivante

Pour rechercher et filtrer des exemples de code pour d'autres produits Google Cloud, consultez l'explorateur d'exemples Google Cloud.