示例#1
0
def create_site_key(project_id: str, domain_name: str) -> str:
    """Create reCAPTCHA Site key which binds a domain name to a unique key.
    Args:
    project_id : GCloud Project ID.
    domain_name: Specify the domain name in which the reCAPTCHA should be activated.
    """
    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    # Set the type of the reCAPTCHA to be displayed.
    # For different types, see: https://cloud.google.com/recaptcha-enterprise/docs/keys
    web_settings = recaptchaenterprise_v1.WebKeySettings()
    web_settings.allowed_domains.append(domain_name)
    web_settings.allow_amp_traffic = False
    web_settings.integration_type = web_settings.IntegrationType.SCORE

    key = recaptchaenterprise_v1.Key()
    key.display_name = "any descriptive name for the key"
    key.web_settings = web_settings

    # Create the request.
    request = recaptchaenterprise_v1.CreateKeyRequest()
    request.parent = f"projects/{project_id}"
    request.key = key

    # Get the name of the created reCAPTCHA site key.
    response = client.create_key(request)
    recaptcha_site_key = response.name.rsplit("/", maxsplit=1)[1]
    print("reCAPTCHA Site key created successfully. Site Key: " +
          recaptcha_site_key)
    return recaptcha_site_key
async def sample_update_key():
    # Create a client
    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceAsyncClient()

    # Initialize request argument(s)
    key = recaptchaenterprise_v1.Key()
    key.web_settings.integration_type = "INVISIBLE"

    request = recaptchaenterprise_v1.UpdateKeyRequest(key=key, )

    # Make the request
    response = await client.update_key(request=request)

    # Handle the response
    print(response)
def update_site_key(project_id: str, recaptcha_site_key: str,
                    domain_name: str) -> None:
    """
    Update the properties of the given site key present under the project id.

    Args:
    project_id: GCloud Project ID.
    recaptcha_site_key: Specify the site key.
    domain_name: Specify the domain name for which the settings should be updated.
    """

    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    # Construct the key details.
    key_name = f"projects/{project_id}/keys/{recaptcha_site_key}"

    # Set the name and the new settings for the key.
    web_settings = recaptchaenterprise_v1.WebKeySettings()
    web_settings.allow_amp_traffic = True
    web_settings.allowed_domains.append(domain_name)

    key = recaptchaenterprise_v1.Key()
    key.display_name = "any descriptive name for the key"
    key.name = key_name
    key.web_settings = web_settings

    update_key_request = recaptchaenterprise_v1.UpdateKeyRequest()
    update_key_request.key = key
    client.update_key(update_key_request)

    time.sleep(5)

    # Retrieve the key and check if the property is updated.
    get_key_request = recaptchaenterprise_v1.GetKeyRequest()
    get_key_request.name = key_name
    response = client.get_key(get_key_request)
    web_settings = response.web_settings

    # Get the changed property.
    if not web_settings.allow_amp_traffic:
        print(
            "Error! reCAPTCHA Site key property hasn't been updated. Please try again !"
        )
    else:
        print("reCAPTCHA Site key successfully updated ! ")
def sample_create_key():
    # Create a client
    client = recaptchaenterprise_v1.RecaptchaEnterpriseServiceClient()

    # Initialize request argument(s)
    key = recaptchaenterprise_v1.Key()
    key.web_settings.integration_type = "INVISIBLE"

    request = recaptchaenterprise_v1.CreateKeyRequest(
        parent="parent_value",
        key=key,
    )

    # Make the request
    response = client.create_key(request=request)

    # Handle the response
    print(response)