I try to get a response from my DialogFlow Agent (v2!) via PHP curl... So I copied the normal curl command from DialogFlow:
curl -H "Content-Type: application/json; charset=utf-8" -H "Authorization: Bearer ya29.xxxxhWdzg3rA" -d "{\"queryInput\":{\"text\":{\"text\":\"Gib mir Informationen über meine Dienstreise von Hamburg\",\"languageCode\":\"de\"}},\"queryParams\":{\"timeZone\":\"Europe/Berlin\"}}" "https://dialogflow.googleapis.com/v2/projects/xxxx/agent/sessions/edd016e1-9xxxxxf3787f:detectIntent"
and "converted" it into PHP:
$query = "Gib mir Informationen über meine Dienstreise nach Hamburg";
$postData = array(
'queryInput' => array(
'text' => array(
'text' => $query,
'languageCode' => 'de'
)),
'queryParams' => array(
'timeZone' => 'Europe/Berlin'
)
);
$jsonData = json_encode($postData);
$ch = curl_init('https://dialogflow.googleapis.com/v2/projects/xxxx/agent/sessions/edd016e1xxxxx7f:detectIntent');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json; charset=utf-8', 'Authorization: Bearer 6cXXXXfe'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
echo $result;
curl_close($ch);
"6cXXXXfe" theres my "Client access token" from DialogFlow. The URL in curl_init() is from the curl command before - I'm pretty sure the mistake is there but I just can't figure out which URL should be there...
I get an error as result:
{
"error":
{
"code": 401,
"message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
"status": "UNAUTHENTICATED"
}
}
Hope someone could help me with that. Thanks!