迁移到 v1 API

本文档介绍了如何从 v1beta1 版本迁移到 v1 版本的 reCAPTCHA Enterprise API。

如果您使用 v1beta1 创建评估并为其添加注解,我们建议您使用 v1,因为 reCAPTCHA Enterprise 帐号卫士等新功能仅在 v1 上提供。v1 同时支持 API 密钥和服务帐号身份验证。

如需将评估调用从 v1beta1 迁移到 v1,请执行以下操作:

  1. 替换通话以创建评估。

    https://recaptchaenterprise.googleapis.com/v1beta1/projects/PROJECT_ID/assessments?key=API_KEY 替换为 https://recaptchaenterprise.googleapis.com/v1/projects/PROJECT_ID/assessments?key=API_KEY

  2. 了解评估的 JSON 响应的变化:

    使用 v1 时,reasonsscore 字段会封装在响应中 riskAnalysis 中;而当您使用 v1beta1 时,reasonsscore 字段不会封装在响应中。

    v1

    {
    "event":{
      "expectedAction":"EXPECTED_ACTION",
      "hashedAccountId":"ACCOUNT_ID",
      "siteKey":"SITE_KEY",
      "token":"TOKEN",
      "userAgent":"(USER-PROVIDED STRING)",
      "userIpAddress":"USER_PROVIDED_IP_ADDRESS"
    },
    "name":"ASSESSMENT_ID",
     "riskAnalysis":{
      "reasons":[],
      "score":"SCORE
    },
    "tokenProperties":{
      "action":"USER_INTERACTION",
      "createTime":"TIMESTAMP",
      "hostname":"HOSTNAME",
      "invalidReason":"(ENUM)",
      "valid":(BOOLEAN)
    }
    }
    

    v1beta1

    {
    "event":{
      "expectedAction":"EXPECTED_ACTION",
      "hashedAccountId":"ACCOUNT_ID",
      "siteKey":"SITE_KEY",
      "token":"TOKEN",
      "userAgent":"(USER-PROVIDED STRING)",
      "userIpAddress":"USER_PROVIDED_IP_ADDRESS"
    },
    "name":"ASSESSMENT_ID",
    "reasons":[],
    "score":"SCORE",
    
    "tokenProperties":{
      "action":"USER_INTERACTION",
      "createTime":"TIMESTAMP",
      "hostname":"HOSTNAME",
      "invalidReason":"(ENUM)",
      "valid":(BOOLEAN)
    }
    }
    

  3. 更新您的环境,以使用 v1 的 JSON 响应中的 reasonsscore 字段。

    以下示例代码显示了 v1v1beta1 的响应中使用的字段:

    v1

    
    .....
    .....
    # Get the risk score and the reason(s)
    for reason in response.risk_analysis.reasons:
        print(reason)
    print(
        "The reCAPTCHA score for this token is: "
        + str(response.risk_analysis.score)
    )
    ....
    ....
    
    

    v1beta1

    
    .....
    .....
    # Get the risk score and the reason(s)
    for reason in response.reasons:
        print(reason)
    print(
        "The reCAPTCHA score for this token is: "
        + str(response.score)
    )
    ....
    ....
    
    
  4. 替换对注解添加注解的调用。

    https://recaptchaenterprise.googleapis.com/v1beta1/projects/PROJECT_ID/assessments}:annotate 替换为 https://recaptchaenterprise.googleapis.com/v1/projects/PROJECT_ID/assessments}:annotate

后续步骤