3

I am trying to implement Recaptcha Enterprise, I get the userToken and am attempting to create an assessment but when sending the information get the following response:

{
"error": {
"code": 400,
"message": "Invalid JSON payload received. Unknown name "{JSON SENT}": Cannot bind query parameter. Field '{JSON SENT}' could not be found in request message.",
"status": "INVALID_ARGUMENT",
"details": [
  {
    "@type": "type.googleapis.com/google.rpc.BadRequest",
    "fieldViolations": [
      {
        "description": "Invalid JSON payload received. Unknown name \"{JSON SENT}' could not be found in request message."
      }
    ]
  }
]
}
}

My php curl file:

 <?php

 $token = $_GET["token"];
 $secret = "SECRET_ID";

 $url = "https://recaptchaenterprise.googleapis.com/v1/projects/{project_id}/assessments?key=" . $secret;

 $curl = curl_init($url);
 curl_setopt($curl, CURLOPT_URL, $url);
 curl_setopt($curl, CURLOPT_POST, true);
 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

 $headers = array(
"Content-Type: application/x-www-form-urlencoded",
 );
 curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

 $data = ['event' => ['token' => $token, 'siteKey' => $secret, 'expectedAction' => 'verify']];

 curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));

 $resp = curl_exec($curl);
 curl_close($curl);
 echo ($resp);

My Json

\"{\"event\":
{\"token\":\"TOKEN\",
\"siteKey\":\"SITE_KEY\",
\"expectedAction\":\"verify\"}
}\"

I have tried consulting the enterprise docs but have been unsuccessful. Any and all help would be appreciated.

  • It may be your expectedAction set to "verify". Here is a list of actions: https://cloud.google.com/recaptcha-enterprise/docs/actions Give "LOGIN" a try and see if it gets past the error. – havoc1 May 12 '22 at 18:16
  • Did you manage to solve this issue by any chance? – user2580 Oct 16 '22 at 15:59

1 Answers1

1
  1. For the call to googleapis.com you'd have to use an api key, not the site key. Use the site key (your $secret) only where you build the $data array.

  2. You are sending JSON to google, so the Content-Type should be 'application/json; chartype=utf-8'.

  3. In case you are also posting JSON from your website to this php, then you'd need to use

    $json=file_get_contents('php://input');
    $jdata=json_decode($json);

to retrieve the data, not $_GET[]. Then you could address:

    $token=$jdata->token
    $action=$jdata->action
jamacoe
  • 519
  • 4
  • 16