微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

CreateAssessment() 上的 Google.Cloud.RecaptchaEnterprise 错误 - “请求包含无效参数”

如何解决CreateAssessment() 上的 Google.Cloud.RecaptchaEnterprise 错误 - “请求包含无效参数”

我正在尝试使用 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="请求包含无效参数。",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}")

解决方法

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

来自the documentation

将在其中创建评估的项目的名称,格式为“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)
};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?