2

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!

1 Answer 1

1

The Client Access Token method is no longer used with the V2 API.

Instead, you should be using OAuth 2 for Server-Server Applications. In this scheme, you will have created a Service Account and downloaded the keys for this account. If you're doing it manually (which you would if you're using curl) before your request you would

  • Using the private key, create a JWT request and send this to Google's auth server to get a limited-time auth token.

  • Include this token in the Authentication: Bearer header in the HTTPS request to Dialogflow.

You can also do this using the gcloud command once you've downloaded the keys.

You might be better off looking into a client library for Dialogflow which will also take care of this. You will still need to get the Google Cloud Service Account and download the key file, but it will take care of the rest.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you so much for your answer. I just followed the „tutorial“ on cloud.google.com/dialogflow-enterprise/docs/reference/… for PHP, but I had some problems on the way. I don’t know if it’s right but I tried to write export GOOGLE_APPLICATION_CREDENTIALS=„pathtomyprivatekeyfile“in my console in my project directory, but it happened nothing… I used the example on the site for using the client library, called it „test.php“ and put it in the root directory of my project directory. (I’m not sure if that’s even the right place for that file?)
Below the function definition of detect_intent_text I called the function with the required parameters. But then I get an error Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials - but the link does not really help me…
We have to code a chatbot with dialogflow as a project in university. Everything worked fine, my intent calls a webhook on my server so it could return data from my database as a response from the chatbot. But now I want to have the queries from the user and the responses from the chatbot in an own „chat interface“ on my site and that’s just horrible….
I've got it! I just had to send a parameter with the path to my json-keyfile... now everything works. Thank you for leading me on the right way! :D $test = array('credentials' => 'my-weather.json'); $sessionsClient = new SessionsClient($test);
Can you please share the complete working code? I am not able to run.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.