Google.Cloud.RecaptchaEnterprise CreateAssessment() 错误 - "Request contains an invalid argument"

Google.Cloud.RecaptchaEnterprise error on CreateAssessment() - "Request contains an invalid argument"

我正在尝试使用 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":"Error received from peer 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":"Request contains an invalid argument.","grpc_status":3}")

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

来自the documentation

The name of the project in which the assessment will be created, in the format "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)
};