Get a reCAPTCHA site key
Stay organized with collections
Save and categorize content based on your preferences.
Get reCAPTCHA site keys for the given the project ID.
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,["# Get a reCAPTCHA site key\n\nGet reCAPTCHA site keys for the given the project ID.\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.api.core.https://cloud.google.com/java/docs/reference/api-common/latest/com.google.api.core.ApiFuture.html;\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.GetKeyRequest.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 java.io.IOException;\n import java.util.concurrent.ExecutionException;\n import java.util.concurrent.TimeUnit;\n import java.util.concurrent.TimeoutException;\n\n public class GetSiteKey {\n\n public static void main(String[] args)\n throws IOException, InterruptedException, ExecutionException, TimeoutException {\n // TODO(developer): Replace these variables before running the sample.\n String projectID = \"your-project-id\";\n String recaptchaSiteKey = \"recaptcha-site-key\";\n\n getSiteKey(projectID, recaptchaSiteKey);\n }\n\n /**\n * Get the reCAPTCHA site key present under the project ID.\n *\n * @param projectID: GCloud Project ID.\n * @param recaptchaSiteKey: Specify the site key to get the details.\n */\n public static void getSiteKey(String projectID, String recaptchaSiteKey)\n throws IOException, InterruptedException, ExecutionException, TimeoutException {\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 // Construct the \"GetSiteKey\" request.\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.GetKeyRequest.html getKeyRequest =\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.GetKeyRequest.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 // Wait for the operation to complete.\n ApiFuture\u003cKey\u003e futureCall = client.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.cloud.recaptchaenterprise.v1.RecaptchaEnterpriseServiceClient.html#com_google_cloud_recaptchaenterprise_v1_RecaptchaEnterpriseServiceClient_getKeyCallable__().futureCall(getKeyRequest);\n https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.Key.html key = futureCall.get(5, TimeUnit.SECONDS);\n\n System.out.println(\"Successfully obtained the key !\" + key.https://cloud.google.com/java/docs/reference/google-cloud-recaptchaenterprise/latest/com.google.recaptchaenterprise.v1.Key.html#com_google_recaptchaenterprise_v1_Key_getName__());\n }\n }\n }\n\n### PHP\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 use Google\\ApiCore\\ApiException;\n use Google\\Cloud\\RecaptchaEnterprise\\V1\\Client\\RecaptchaEnterpriseServiceClient;\n use Google\\Cloud\\RecaptchaEnterprise\\V1\\GetKeyRequest;\n use Google\\Cloud\\RecaptchaEnterprise\\V1\\WebKeySettings\\IntegrationType;\n\n /**\n * Get a reCAPTCHA key from a google cloud project\n *\n * @param string $projectId Your Google Cloud project ID\n * @param string $keyId The 40 char long key ID you wish to fetch\n */\n function get_key(string $projectId, string $keyId): void\n {\n $client = new RecaptchaEnterpriseServiceClient();\n $formattedKeyName = $client-\u003ekeyName($projectId, $keyId);\n\n try {\n // Returns a 'Google\\Cloud\\RecaptchaEnterprise\\V1\\Key' object\n $getKeyRequest = (new GetKeyRequest())\n -\u003esetName($formattedKeyName);\n $key = $client-\u003egetKey($getKeyRequest);\n $webSettings = $key-\u003egetWebSettings();\n\n print('Key fetched' . PHP_EOL);\n printf('Display name: %s' . PHP_EOL, $key-\u003egetDisplayName());\n // $key-\u003egetCreateTime() returns a Google\\Protobuf\\Timestamp object\n printf('Create time: %d' . PHP_EOL, $key-\u003egetCreateTime()-\u003egetSeconds());\n printf('Web platform settings: %s' . PHP_EOL, $key-\u003ehasWebSettings() ? 'Yes' : 'No');\n printf('Allowed all domains: %s' . PHP_EOL, $key-\u003ehasWebSettings() && $webSettings-\u003egetAllowAllDomains() ? 'Yes' : 'No');\n printf('Integration Type: %s' . PHP_EOL, $key-\u003ehasWebSettings() ? IntegrationType::name($webSettings-\u003egetIntegrationType()) : 'N/A');\n } catch (ApiException $e) {\n if ($e-\u003egetStatus() === 'NOT_FOUND') {\n printf('The key with Key ID: %s doesn\\'t exist.' . PHP_EOL, $keyId);\n } else {\n print('getKey() call failed with the following error: ');\n print($e);\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\n def get_site_key(project_id: str, recaptcha_site_key: str) -\u003e None:\n \"\"\"\n Get the reCAPTCHA site key present under the project ID.\n\n Args:\n project_id: GCloud Project ID.\n recaptcha_site_key: Specify the site key to get the details.\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 # Construct the key details.\n key_name = f\"projects/{project_id}/keys/{recaptcha_site_key}\"\n\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.GetKeyRequest.html()\n request.name = key_name\n\n key = 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_get_key(request)\n print(\"Successfully obtained the key !\" + key.name)\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 # Get details of site key registered to use recaptcha services.\n #\n # @param project_id [String] GCloud Project ID.\n # @param site_key [String] Site key to be updated.\n # @return [void]\n def get_site_key project_id:, site_key:\n # Create the reCAPTCHA client.\n client = ::Google::Cloud::RecaptchaEnterprise.recaptcha_enterprise_service\n\n key = client.get_key name: \"projects/#{project_id}/keys/#{site_key}\"\n puts \"Successfully obtained the key ! #{key.name}\"\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)."]]