- Thi
- Official documentation
- Wanna run the Node.js samples?{:#run-samples}
- Wanna try
gapi
(JS client)? - Dialogflow REST APIs with Postman
Thi
Google’s documentation is like an ocean. It’s not easy to find a right one to start. This note contains only basic things that I’ve already worked with. Trying your own hand at Google’s APIs will help you understand more.
👉 Note: Google OAuth 2.0 with NodeJS. 👉 Repo: dinhanhthi/google-api-playground (private).
Official documentation
click to open
-
APIs & references -- the root of all things.
- Node.js client
library -- wanna use in a backend?
- Dialogflow SDK Client Reference
- googleapis/nodejs-dialogflow
-- Github repo.
- Samples -- wanna run these? Step to this section.
- REST APIs -- wanna use
GET
,POST
,...?
- Node.js client
library -- wanna use in a backend?
-
💡 Tip:
us-dialogflow.googleapis.com
anddialogflow.googleapis.com
are the same, so you can use<location>-dialogflow.googleapis.com
in your codes. -
Available regions (used in
locations
).💡 Tip:
<region>-dialogflow.googleapis.com
= endpoint. -
google/google-api-javascript-client -- aka
gapi
. Github repo. -
Understand roles -- If you decide to create a service account, you will need to assign a role to some users/emails. Each role has different rights to use your data.
Wanna run the Node.js samples?{:#run-samples}
👉 Link of all samples on github.
The old version uses dialogflow
and @type/dialogflow
. The new version uses only one @google-cloud/dialogflow
!
:::
steps
-
Create a folder, eg.
/home/thi/df-samples/
-
If you come from Dialogflow Console > choose an agent > click on the gear next to the its name > Click on "Project ID" to open Google Cloud Platform Console.
-
If you come from GCP Console, it's the same.
-
Follow these steps to generate a JSON key (you'll download a JSON file). Store your JSON file in
df-samples/credential.json
. Note down the project_id, we will use it later, eg.project_abc
. -
Run each time you wanna test
export GOOGLE_APPLICATION_CREDENTIALS="/home/thi/df-samples/credential.json"
OR save this line to/home/thi/.bashrc
or/home/thi/.zshrc
(if you use ZSH) and then refresh the current terminal (with this method, you don't need to run again previous line).Alternative: You don't have to use system variable
GOOGLE_APPLICATION_CREDENTIALS
if you don't want. Incredential.json
, copyprivate_key
andclient_email
and then use them as,const client = new AgentsClient({
credentials: { private_key, client_email }
}); -
Go to
/df-samples/
and runnpm i @google-cloud/dialogflow
. -
Try this quickstart.js.
-
On terminal, run
node quickstart.js project_abc
-
Read carefully the content of each file in samples, you have to put the corresponding inputs for the sample to work!
try something outside 'samples'
In case you wanna try something outside the files given in samples. Check
this SDK. Suppose we wanna try this one -- AgentsClient.searchAgents()
- Make the same things in "Step by step". At step 7, create
search-agents.js
with the same content assamples/set-agent.js
. We are going to change this file. - Read the reference, change the input. Here is an example,
Then run `node search-agents.js`.
Different locations?
The example in "Try something outside..." gives us an example of using different regions. Below are some remarks:
-
On DF console, you can create some agents in a different regions, default is
global
(orus
). -
On the Google's documentations, they don't mention about the usage of location. If they say
parent = "projects/-"
, we shoud useparent = "projects/-" + "/locations/" + location
wherelocation
can be one of "Region ID". -
Change also the endpoint, option
apiEndpoint
inAgentsClient
's constructor, for example.const client = new AgentsClient({
apiEndpoint: location + "-dialogflow.googleapis.com",
});
Wanna try gapi
(JS client)?
Google has announced that they will be discontinuing the Google Sign-In JavaScript Platform Library for web. You will have to switch to using Google Identity Services (or Sign In With Google or gsi
). The old service will be completely discontinued on March 31, 2023.
What’s this gapi
? You can use it completely inside an HTML file without using any backend.
👉 List of samples. 👉 You have to use REST API in this case.
💡 Tip: If you are using VSCode, you can install the Live Server extension to quickly create a server (port 5500
by default). Just click the button at the bottom right of the file and a website will appear.
Steps
- For setting up, follow these steps.
- After that, you should obtain an
API_KEY
and anCLIENT_ID
. - First, try this sample.
- Using something like Live Server and
open
authSample.html
. - Make a test.
- Make sure you create the "OAuth consent screen" before you create "OAuth 2.0 Client IDs". The "consent screen" is the popup window that contains all the information about the scopes your app will ask users for permission.
- Make sure you add
http://localhost:5500
(which is created in step 4) to "Authorized JavaScript origins" and "Authorized redirect URIs". You may have to wait a few "ten minutes" for everything to work. Without this step, you may encounter the errormismatch_uri
.
- Make sure you create the “OAuth consent screen” before you create “OAuth 2.0 Client IDs”. The “consent screen” is the popup window that contains all the information about the scopes your app will ask users for permission.
- Make sure you add
http://localhost:5500
(which is created in step 4) to “Authorized JavaScript origins” and “Authorized redirect URIs”. You may have to wait a few “ten minutes” for everything to work. Without this step, you may encounter the errormismatch_uri
.
Examples
projects.agent.search
GET https://{endpoint}/v2/{parent=projects/*}/agent:search
- REST: .../v2/projects.agent/search
- SDK: .../v2.AgentsClient.html#searchAgents
projects.agent.sessions.detectIntent
POST https://{endpoint}/v2/{session=projects/*/locations/ */agent/sessions/*}:detectIntent
- REST: .../v2/projects.agent.sessions/detectIntent
- SDK: .../v2.SessionsClient.html#detectIntent :::
Dialogflow REST APIs with Postman
Additional configurations
- Create a collection and add the Authorization for this collection. All of its request will use the same auth method.
- Create variables (on tab "Variables") to store "CLIENT ID" (
client_id
) and "CLIENT SECRET" (asclient_secret
), then use them in the form byand
.
The following wiki, pages and posts are tagged with
Title | Type | Excerpt |
---|---|---|
2021-09-26-googlesheet.md | post | Google Sheets API with github Pages and API |
Google's Dialogflow APIs | post | Sat, Oct 30, 21, google offical doc and setup instruction |
Pilot's preflight checklist FAA | post | Tue, Nov 09, 21, preflight checklist with data mining, d3 visualization and google sheet implementation |
D3js trial and errors | post | Thursday, trying to integrate with jekyll and data mining |
D3 square.github.io data binding | post | Thu, Nov 11, 21, data binding |
D3.js square.github.io tutorial | post | Thu, Nov 11, 21, data binding |
idrather api course | post | Tue, Nov 16, 21, preparation setup and tools necessary for this api course |
hosted search engine | post | Thu, Nov 18, 21, API to let you seamlessly implement search within the site, mobile and voice applications |
Weather app from firebase | post | Sunday-weather-app, open weather api |
GoogleAppsScript-Category.md | post | 카테고리 2단계 (끝) |
OpenApi-usage.md | post | 실제 사용법 정리 |