Skip to content

Instantly share code, notes, and snippets.

@dinhanhthi
Created October 29, 2021 18:03
Show Gist options
  • Save dinhanhthi/e57e00886adaa611e2b49f9dcf76d90e to your computer and use it in GitHub Desktop.
Save dinhanhthi/e57e00886adaa611e2b49f9dcf76d90e to your computer and use it in GitHub Desktop.
Using Google REST API with NodeJS
// Obtain JSON first: https://cloud.google.com/docs/authentication/getting-started#creating_a_service_account
// How to use: node search-agents-json-rest.js
// Search agents API: https://cloud.google.com/dialogflow/es/docs/reference/rest/v2/projects.agent/search
require("dotenv").config();
const { google } = require("googleapis");
const request = require("request");
async function main() {
var token;
const auth = new google.auth.GoogleAuth({
keyFilename: "<absolute-path-to-json-file>",
// Scopes can be specified either as an array or as a single, space-delimited string.
scopes: ["https://www.googleapis.com/auth/dialogflow"],
});
async function getAccessToken() {
const authClient = await auth.getClient();
token = await authClient.getAccessToken();
return token;
}
await getAccessToken();
const location = "global";
const parent = (location) => "projects/-" + "/locations/" + location;
const parentPath = parent(location);
request.get(
`https://dialogflow.googleapis.com/v2/projects/-/agent:search`,
{
auth: {
bearer: token.token,
},
},
function (err, res) {
if (err) {
console.error(err);
} else {
console.log(res.body);
}
}
);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment