2

How to I get a bearer token for a Dialogflow v2beta1 API call?

I want to integrate Dialogflow APIs so now I can't even test APIs in postman without bearer token. For testing I have generated API Key for my agent in GCP project but I didn't found any solution for getting bearer token.

POST https://dialogflow.googleapis.com/v2beta1/[PARENT]/intents?key=[YOUR_API_KEY] HTTP/1.1

Authorization: Bearer [YOUR_ACCESS_TOKEN] Accept: application/json Content-Type: application/json
Wytrzymały Wiktor
  • 11,492
  • 5
  • 29
  • 37

1 Answers1

0

I guess you already have a Service Account with proper permissions to project/product/resource. If no, you can find a guide on how to create it in Creating and managing service accounts.

Regarding Bearer Token you should read about it in Authenticating as a service account.

If you have a Service Account with proper access and key.json you can use Bearer token. In GCP console you can print default token using command:

### for default SA
$ gcloud auth application-default print-access-token

### for other SA
$ gcloud auth print-access-token SA_NAME@PROJECT_ID.iam.gserviceaccount.com

More details can be found in this docs.

Request for default SA should looks like this:

 curl -X POST /v2beta1/{parent=projects/*}/agent:train \
 -H "Authorization: Bearer $(gcloud auth application-default print-access-token)"

For specific one time request you should use below example:

  curl -X POST /v2beta1/{parent=projects/*}/agent:train \
 -H "Authorization: Bearer $(gcloud auth print-access-token <YourSAaccount>)"

SA account might looks like: <SAname>@<projectID>.iam.gserviceaccount.com

Please keep in mind that this SA must be active. You can do it using command to activate SA:

$ gcloud auth activate-service-account SA_NAME@PROJECT_ID.iam.gserviceaccount.com --key-file=/path/to/SAkey/key.json

###or using just key
$ gcloud auth activate-service-account --key-file=/path/to/SAkey/key.json

And command for listing active SA is:

$ gcloud auth list

I chose a random POST from Dialogflow API.

PjoterS
  • 12,841
  • 1
  • 22
  • 54