在非 Google Cloud 环境中设置 reCAPTCHA Enterprise

在非 Google Cloud 环境中设置 reCAPTCHA Enterprise

本快速入门介绍如何在支持外部身份验证方法(如服务帐号)的 Google Cloud、本地、CRM 或 SaaS 环境以外的云上设置 reCAPTCHA Enterprise。

在非 Google Cloud 环境中设置 reCAPTCHA Enterprise 需要启用 reCAPTCHA Enterprise API、创建服务账号以及为后端配置服务账号。

须知事项

在 Google Cloud Console 中的项目选择器页面上,选择或创建一个 Google Cloud 项目

转到“项目选择器”

控制台

  1. 在 Google Cloud 控制台中,转到 reCAPTCHA Enterprise API 页面。

    转到 reCAPTCHA Enterprise API

  2. 验证项目名称是否显示在页面顶部的项目选择器中。

    如果您没有看到项目名称,请点击项目选择器,然后选择您的项目。

  3. 点击启用

gcloud

  1. 在 Google Cloud 控制台中,激活 Cloud Shell。

    激活 Cloud Shell

    Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。

  2. 如需在 gcloud 会话中设置 Google Cloud 项目,请运行 gcloud config set project 命令。将 PROJECT_ID 替换为您的 Google Cloud 项目 ID。
           gcloud config set project PROJECT_ID
           
  3. 如需启用 reCAPTCHA Enterprise API,请运行 gcloud services enable 命令:
             gcloud services enable recaptchaenterprise.googleapis.com
           
  4. 如需验证 reCAPTCHA Enterprise API 是否已启用,请运行 gcloud services list 命令:
             gcloud services list --enabled
           

    验证 reCAPTCHA Enterprise API 是否在已启用的 API 列表中列出。

配置服务帐号以进行后端身份验证

您可以使用 reCAPTCHA Enterprise 客户端库或 Google Cloud CLI 配置您为后端身份验证创建的新服务帐号。

客户端库

  1. 通过设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 向应用代码提供身份验证凭据。此变量仅适用于当前的 Shell 会话。如果您希望变量应用于未来的 Shell 会话,请在 shell 启动文件中设置变量,例如在 ~/.bashrc~/.profile 文件中。

    Linux 或 macOS

    export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

    例如:

    export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

    Windows

    对于 PowerShell:

    $env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

    例如:

    $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

    对于命令提示符:

    set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

  2. 安装客户端库。

    C#

    如需了解详情,请参阅设置 C# 开发环境
    Install-Package Google.Cloud.RecaptchaEnterprise.V1
    

    Go

    如需了解详情,请参阅设置 Go 开发环境
    go get cloud.google.com/go/recaptchaenterprise/apiv1
    

    Java

    如需了解详情,请参阅设置 Java 开发环境

    If you are using Maven, add the following to your pom.xml file. For more information about BOMs, see The Google Cloud Platform Libraries BOM.

    <dependencyManagement>
      <dependencies>
        <dependency>
          <groupId>com.google.cloud</groupId>
          <artifactId>libraries-bom</artifactId>
          <version>26.37.0</version>
          <type>pom</type>
          <scope>import</scope>
        </dependency>
      </dependencies>
    </dependencyManagement>
    
    <dependencies>
      <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-recaptchaenterprise</artifactId>
      </dependency>

    If you are using Gradle, add the following to your dependencies:

    implementation 'com.google.cloud:google-cloud-recaptchaenterprise:3.38.0'

    If you are using sbt, add the following to your dependencies:

    libraryDependencies += "com.google.cloud" % "google-cloud-recaptchaenterprise" % "3.38.0"

    If you're using Visual Studio Code, IntelliJ, or Eclipse, you can add client libraries to your project using the following IDE plugins:

    The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.

    Node.js

    如需了解详情,请参阅设置 Node.js 开发环境
    npm install --save @google-cloud/recaptcha-enterprise
    

    PHP

    如需了解详情,请参阅在 Google Cloud 上使用 PHP。将该库作为依赖项添加到您的 composer.json 文件中:
    composer require google/cloud-recaptcha-enterprise
    
    或者,您也可以在本地下载软件包。

    Python

    如需了解详情,请参阅设置 Python 开发环境
    pip install --upgrade google-cloud-recaptcha-enterprise
    

    Ruby

    如需了解详情,请参阅设置 Ruby 开发环境
    gem install google-cloud-recaptcha_enterprise
    

现在,您创建的服务账号可以用于后端身份验证。创建评估时,您可以使用 reCAPTCHA Enterprise 客户端库对服务账号进行身份验证。

gcloud CLI

  1. 安装 Google Cloud CLI,然后通过运行以下命令初始化 Google Cloud CLI:

    gcloud init
  2. 通过设置环境变量 GOOGLE_APPLICATION_CREDENTIALS 向应用代码提供身份验证凭据。此变量仅适用于当前的 Shell 会话。如果您希望变量应用于未来的 Shell 会话,请在 shell 启动文件中设置变量,例如在 ~/.bashrc~/.profile 文件中。

    Linux 或 macOS

    export GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

    例如:

    export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"

    Windows

    对于 PowerShell:

    $env:GOOGLE_APPLICATION_CREDENTIALS="KEY_PATH"

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

    例如:

    $env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"

    对于命令提示符:

    set GOOGLE_APPLICATION_CREDENTIALS=KEY_PATH

    KEY_PATH 替换为包含凭据的 JSON 文件的路径。

现在,您创建的服务账号可以用于后端身份验证。创建评估时,您可以使用 gcloud CLI 对服务帐号进行身份验证。

后续步骤

  1. 确定要使用的 reCAPTCHA(网站)密钥的类型创建 reCAPTCHA 密钥
  2. 在网页上安装基于得分的网站密钥或复选框网站密钥。

    将 reCAPTCHA Enterprise 与 Android 应用iOS 应用集成。

  3. 创建评估

  4. 解读评估,并根据得分对您的网站采取适当的措施。