1

我正在尝试使用 Google.Cloud.RecaptchaEnterprise 库来验证我的客户获得的新企业密钥的验证码请求。

string _siteKey = ConfigurationManager.AppSettings["GoogleCaptcha.CheckboxCaptcha.SiteKey"];
string _apiKey = ConfigurationManager.AppSettings["GoogleCaptcha.ApiKey"];
string _projectId = ConfigurationManager.AppSettings["GoogleCaptcha.ProjectId"];
string recaptchaAction = "CreateAccountAssessment";
try {
    var appPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;
    string credential_path = appPath + "googlecredentials.json";
    System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS", credential_path);

    RecaptchaEnterpriseServiceClient client =
    RecaptchaEnterpriseServiceClient.Create();

    CreateAssessmentRequest createAssessmentRequest = new CreateAssessmentRequest()
    {
        Assessment = new Assessment()
        {
            Event = new Event()
            {
                SiteKey = _siteKey,
                Token = formResponse,
                ExpectedAction = "Create_Account"
            },
            Name = recaptchaAction,                         
        },
        Parent = _projectId
    };

    Assessment response = client.CreateAssessment(createAssessmentRequest);

    if (response.TokenProperties.Valid == false)
    {
        Sitecore.Diagnostics.Log.Error("The CreateAssessment() call failed " +
            "because the token was invalid for the following reason: " +
            response.TokenProperties.InvalidReason.ToString(), this);

        return "Invalid captcha.";
    }
    else
    {
        if (response.Event.ExpectedAction == recaptchaAction)
        {
            Sitecore.Diagnostics.Log.Error("The reCAPTCHA score for this token is: " +
                response.RiskAnalysis.Score.ToString(), this);

            return "";
        }
        else
        {
            Sitecore.Diagnostics.Log.Error("The action attribute in your reCAPTCHA " +
                "tag does not match the action you are expecting to score", this);

            return "Invalid captcha.";

        }

    }
}
catch (Exception ex)
{
    Sitecore.Diagnostics.Log.Error("Error validating captcha on " + _url + "; " + ex.Message, this);
    return "Unable to connect to captcha service.";
}

据我所知,我的所有属性都是正确的,但它会引发错误Assessment response = client.CreateAssessment(createAssessmentRequest);

Status(StatusCode="InvalidArgument", Detail="Request contains an invalid argument.", DebugException="Grpc.Core.Internal.CoreErrorDetailException: {"created":"@1621287236.280000000","description":"从对等 ipv6 收到错误:[2607:f8b0:4006:81a::200a]:443","file":"T:\src\github\grpc\workspace_csharp_ext_windows_x64\src\core\lib\surface\call.cc","file_line": 1062,"grpc_message":"请求包含无效参数。","grpc_status":3}")

4

1 回答 1

2

我强烈怀疑问题(或至少问题)是Parent请求的属性。

文档中

将在其中创建评估的项目的名称,格式为“projects/{project}”。

...而我怀疑您的项目 ID只是ID,而不是以“projects/”开头的资源名称。

我建议尽可能使用生成的资源名称类,并带有相应的属性。因此,在这种情况下,您将拥有:

CreateAssessmentRequest createAssessmentRequest = new CreateAssessmentRequest
{
    Assessment = new Assessment
    {
        Event = new Event
        {
            SiteKey = _siteKey,
            Token = formResponse,
            ExpectedAction = "Create_Account"
        },
        Name = recaptchaAction,                         
    },
    ParentAsProjectName = new ProjectName(_projectId)
};
于 2021-05-17T21:42:49.717 回答