Migrate a site key
Stay organized with collections
Save and categorize content based on your preferences.
Migrate a site key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Migrate a site key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to reCAPTCHA, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n import com.google.cloud.recaptchaenterprise.v1.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.html;\n import com.google.recaptchaenterprise.v1.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.Key.html;\n import com.google.recaptchaenterprise.v1.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.KeyName.html;\n import com.google.recaptchaenterprise.v1.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.MigrateKeyRequest.html;\n import java.io.IOException;\n\n public class MigrateKey {\n\n public static void main(String[] args) throws IOException {\n String projectId = \"project-id\";\n String recaptchaSiteKey = \"recaptcha-site-key\";\n\n migrateKey(projectId, recaptchaSiteKey);\n }\n\n /**\n * Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise. If you created the key\n * using Admin console: https://www.google.com/recaptcha/admin/site, then use this API to migrate\n * to reCAPTCHA Enterprise. For more info, see:\n * https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha\n *\n * @param projectId: Google Cloud Project Id.\n * @param recaptchaSiteKey: Specify the site key to migrate.\n */\n public static void migrateKey(String projectId, String recaptchaSiteKey) throws IOException {\n // Initialize client that will be used to send requests. This client only needs to be created\n // once, and can be reused for multiple requests. After completing all of your requests, call\n // the `client.close()` method on the client to safely\n // clean up any remaining background resources.\n try (https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.html client = https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.html.create()) {\n\n // Specify the key name to migrate.\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.MigrateKeyRequest.html migrateKeyRequest =\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.MigrateKeyRequest.html.newBuilder()\n .setName(https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.KeyName.html.of(projectId, recaptchaSiteKey).toString())\n .build();\n\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.Key.html response = client.migrateKey(migrateKeyRequest);\n\n // To verify if the site key has been migrated, use 'ListSiteKeys' and check if the\n // key is present.\n for (https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.Key.html key : recaptcha.ListSiteKeys.listSiteKeys(projectId).iterateAll()) {\n if (key.equals(response)) {\n System.out.printf(\"Key migrated successfully: %s\", recaptchaSiteKey);\n }\n }\n }\n }\n }\n\n### Python\n\n\nTo authenticate to reCAPTCHA, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/\n\n from list_site_keys import list_site_keys\n\n\n def migrate_site_key(project_id: str, recaptcha_site_key: str) -\u003e None:\n \"\"\"Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.\n If you created the key using Admin console: https://www.google.com/recaptcha/admin/site,\n then use this API to migrate to reCAPTCHA Enterprise.\n For more info, see: https://cloud.google.com/recaptcha-enterprise/docs/migrate-recaptcha\n Args:\n project_id: Google Cloud Project ID.\n recaptcha_site_key: Specify the site key to migrate.\n \"\"\"\n\n client = https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/.https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.RecaptchaEnterpriseServiceClient.html()\n\n # Specify the key name to migrate.\n name = f\"projects/{project_id}/keys/{recaptcha_site_key}\"\n request = https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/.https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/google.cloud.recaptchaenterprise_v1.types.MigrateKeyRequest.html()\n request.name = name\n\n response = client.https://cloud.google.com/python/docs/reference/recaptchaenterprise/latest/google.cloud.recaptchaenterprise_v1.services.recaptcha_enterprise_service.RecaptchaEnterpriseServiceClient.html#google_cloud_recaptchaenterprise_v1_services_recaptcha_enterprise_service_RecaptchaEnterpriseServiceClient_migrate_key(request)\n # To verify if the site key has been migrated, use 'list_site_keys' to check if the\n # key is present.\n for key in list_site_keys(project_id):\n if key.name == response.name:\n print(f\"Key migrated successfully: {recaptcha_site_key}\")\n\n### Ruby\n\n\nTo authenticate to reCAPTCHA, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"google/cloud/recaptcha_enterprise\"\n\n # Migrate a key from reCAPTCHA (non-Enterprise) to reCAPTCHA Enterprise.\n #\n # @param project_id [String] GCloud Project ID.\n # @param site_key [String] Site key to be updated.\n # @return [void]\n def migrate_site_key project_id:, site_key:\n # Create the reCAPTCHA client.\n client = ::Google::Cloud::RecaptchaEnterprise.recaptcha_enterprise_service\n\n response = client.migrate_key name: \"projects/#{project_id}/keys/#{site_key}\"\n\n # To verify if the site key has been migrated, use 'list_keys' to check if the\n # key is present.\n keys = client.list_keys parent: \"projects/#{project_id}\"\n keys.each do |key|\n puts \"Key migrated successfully: #{site_key}\" if key.name == response.name\n end\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=recaptcha_enterprise)."]]