How to setup Google Translate API for Node.js? - node.js

I want to use Googles Cloud Translation API in my Node.js application, however I'm getting a The request is missing a valid API key. error.
I have followed the Quickstart guide provided by Google.
I have created GCP project, downloaded the private key as JSON file and setup the environment variable in Powershell (img).
After that I've installed the library with
yarn add #google-cloud/translate
The code I'm running in my translate.js file comes from the Quickstart guide with additional try-catch blocks:
async function quickstart(
projectId = process.env.PROJECT_ID // Project Id from JSON file
) {
try {
// Imports the Google Cloud client library
const { Translate } = require('#google-cloud/translate');
// Instantiates a client
const translate = new Translate({ projectId });
// The text to translate
const text = 'Hello, world!';
// The target language
const target = 'ru';
// Translates some text into Russian
const [translation] = await translate.translate(text, target);
console.log('Text:', text);
console.log('Translation:', translation);
} catch (error) {
console.error(error);
}
}
quickstart();
When I then run node translate.js, I'll get an Error:
{ Error: The request is missing a valid API key.
...
code: 403,
errors:
[ { message: 'The request is missing a valid API key.',
domain: 'global',
reason: 'forbidden' } ],
response: undefined,
message: 'The request is missing a valid API key.' }
I am on Windows 10, Node v10.13.0.

Believe you would have missed this environment variable defining , before starting the node service
Replace [PATH] with the file path of the JSON file that contains your service account key, and [FILE_NAME] with the filename.
With PowerShell:
$env:GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
For example:
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\[FILE_NAME].json"

Related

Node.JS PowerBI App Owns Data for Customers w/ Service Principal (set "config.json" from a table in my database)

I'm attempting to refactor the "Node.JS PowerBI App Owns Data for Customers w/ Service Principal" code example (found HERE).
My objective is to import the data for the "config.json" from a table in my database and insert the "workspaceId" and "reportId" values from my database into the "getEmbedInfo()" function (inside the "embedConfigServices.js" file). Reason being, I want to use different configurations based on user attributes. I am using Auth0 to login users on the frontend, and I am sending the user metadata to the backend so that I can filter the database query by the user's company name.
I am able to console.log the config data, but I am having difficulty figuring out how to insert those results into the "getEmbedInfo()" function.
It feels like I'm making a simple syntax error somewhere, but I am stuck. Here's a sample of my code:
//----Code Snippet from "embedConfigServices.js" file ----//
async function getEmbedInfo() {
try {
const url = ;
const set_config = async function () {
let response = await axios.get(url);
const config = response.data;
console.log(config);
};
set_config();
const embedParams = await getEmbedParamsForSingleReport(
config.workspaceId,
config.reportId
);
return {
accessToken: embedParams.embedToken.token,
embedUrl: embedParams.reportsDetail,
expiry: embedParams.embedToken.expiration,
status: 200,
};
} catch (err) {
return {
status: err.status,
error: err.statusText,
}
};
}
}
This is the error I am receiving on the frontend:
"Cannot read property 'get' of undefined"
Any help would be much appreciated. Thanks in advance.
Carlos
The error is because of fetching wrong URL. The problem is with the config for the Service Principal. We will need to provide reportId, workspaceId for the SPA and also make sure you added the service principal to workspace and followed all the steps from the below documentation for the service principal authentication.
References:
https://learn.microsoft.com/power-bi/developer/embedded/embed-service-principal

Node JS generating access token for Microsoft Graph API on the server is throwing 400 error

I am building a Rest API using Node JS as backend. My API needs to fetch the data from the Microsoft Teams. To do that, I am first trying to generate the access token following this link, https://spin.atomicobject.com/2021/10/08/microsoft-graph-api-node/. But it keeps returning 400 errors.
First I logged into Azure and created an app under App Registrations. Then I created an client secret for the app and also set the API permissions as follow.
In the Node JS backend, I am trying to generate the access token using the code below.
const msClientId = `xxx`;
const msTenantId = `xxx`;
const msClientSecret = `xxx`
async function generateAccessToken () {
try {
const msalConfig = {
auth: {
clientId: msClientId,
clientSecret: msClientSecret,
authority: `https://login.microsoftonline.com/${msTenantId}`,
}
}
const cca = new msal.ConfidentialClientApplication(msalConfig);
const authResponse = await cca.acquireTokenByClientCredential({
scopes: [ `User.Read` ]
});
return authResponse.accessToken;
} catch (e) {
return e.message;
}
}
When I run the code, it is returning the following error.
network_error: Network request failed. Please check network trace to determine root cause. | Fetch client threw: Error: HTTP status code 400 | Attempted to reach: https://login.microsoftonline.com/xxx/oauth2/v2.0/token
What is wrong with my code and how can I fix it?
Have you tried the .default scope?
Like:
scopes: ['https://graph.microsoft.com/.default']
The endpoint you are using in authority is incorrect, could you please use the API:
https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token
Ref doc: https://learn.microsoft.com/en-us/graph/auth-v2-service#token-request

UnhandledPromiseRejectionWarning in google translate api with node.js

const projectId = 'natural-nebula-number';
const {
Translate
} = require('#google-cloud/translate').v2;
const translate = new Translate({
projectId,
});
async function quickStart() {
const text = 'Hello, world!';
const target = 'ru';
const [translation] = await translate.translate(text, target);
console.log(`Text: ${text}`);
console.log(`Translation: ${translation}`);
}
quickStart();
I wrote some code for using google translate api with node.js
i had install #google-cloud/translate. through npm.
and i did copy,paste code in this site google api usage
but i got some error on my command
here is the error message.
(node:440) UnhandledPromiseRejectionWarning: Error: The request is missing a valid API key.
at new ApiError (/mnt/c/Users/U suk jang/Desktop/testinf/node_modules/#google-cloud/common/build/src/util.js:73:15)
at Util.parseHttpRespBody (/mnt/c/Users/U suk jang/Desktop/testinf/node_modules/#google-cloud/common/build/src/util.js:208:38)
at Util.handleResp (/mnt/c/Users/U suk jang/Desktop/testinf/node_modules/#google-cloud/common/build/src/util.js:149:117)
at /mnt/c/Users/U suk jang/Desktop/testinf/node_modules/#google-cloud/common/build/src/util.js:477:22
at onResponse (/mnt/c/Users/U suk jang/Desktop/testinf/node_modules/retry-request/index.js:228:7)
at /mnt/c/Users/U suk jang/Desktop/testinf/node_modules/teeny-request/build/src/index.js:226:13
i wrote actually full projectid (of couse real number).
but they say they cannot find my api key.
how should i give my api key to google
and where is the google-docs for this problem?
You copy-pasted the code in the link provided, but did you read the article and follow all prerequisite steps? It links to another article, Cloud Translation: NodeJS Client. There you will find that you need to pay special attention top step 4 (authentication) mentioned under the Quickstart paragraph. Without it, the used client library will not find your API key.

How to use googleapis google.auth.GoogleAuth() for google API service account in Twilio serverless function?

How to use googleapis google.auth.GoogleAuth() for google API service account in Twilio serverless function, since there is no FS path to provide as a keyFile value?
Based on the example here ( https://www.section.io/engineering-education/google-sheets-api-in-nodejs/ ) and here ( Google api node.js client documentation ) my code is based on the example here ( Receive an inbound SMS ) and looks like...
const {google} = require('googleapis')
const fs = require('fs')
exports.handler = async function(context, event, callback) {
const twiml = new Twilio.twiml.MessagingResponse()
// console.log(Runtime.getAssets()["/gservicecreds.private.json"].path)
console.log('Opening google API creds for examination...')
const creds = JSON.parse(
fs.readFileSync(Runtime.getAssets()["/gservicecreds.private.json"].path, "utf8")
)
console.log(creds)
// connect to google sheet
console.log("Getting googleapis connection...")
const auth = new google.auth.GoogleAuth({
keyFile: Runtime.getAssets()["/gservicecreds.private.json"].path,
scopes: "https://www.googleapis.com/auth/spreadsheets",
})
const authClientObj = await auth.getClient()
const sheets = google.sheets({version: 'v4', auth: authClientObj})
const spreadsheetId = "myspreadsheetID"
console.log("Processing message...")
if (String(event.Body).trim().toLowerCase() == 'KEYWORD') {
console.log('DO SOMETHING...')
try {
// see https://developers.google.com/sheets/api/guides/values#reading_a_single_range
let response = await sheets.spreadsheets.values.get({
spreadsheetId: spreadsheetId,
range: "'My Sheet'!B2B1000"
})
console.log("Got data...")
console.log(response)
console.log(response.result)
console.log(response.result.values)
} catch (error) {
console.log('An error occurred...')
console.log(error)
console.log(error.response)
console.log(error.errors)
}
}
// Return the TwiML as the second argument to `callback`
// This will render the response as XML in reply to the webhook request
return callback(null, twiml)
...where the Asset referenced in the code is for a JSON generated from creating a key pair for a Google APIs Service Account and manually copy/pasting the JSON data as an Asset in the serverless function editor web UI.
I see error messages like...
An error occurred...
{ response: '[Object]', config: '[Object]', code: 403, errors: '[Object]' }
{ config: '[Object]', data: '[Object]', headers: '[Object]', status: 403, statusText: 'Forbidden', request: '[Object]' }
[ { message: 'The caller does not have permission', domain: 'global', reason: 'forbidden' } ]
I am assuming that this is due to the keyFile not being read in right at the auth const declaration (IDK how to do it since all the example I see assume a local filepath as the value, but IDK how to do have the function access that file for a serverless function (my attempt in the code block is really just a shot in the dark)).
FYI, I can see that the service account has an Editor role in the google APIs console (though I notice the "Resources this service account can access" has the error
"Could not fund an ancestor of the selected project where you have access to view a policy report on at least one ancestor"
(I really have no idea what that means or implies at all, very new to this)). Eg...
Can anyone help with what could be going wrong here?
(BTW if there is something really dumb/obvious that I am missing (eg. a typo) just LMK in a comment so can delete this post (as it would then not serve any future value of others))
The caller does not have permission', domain: 'global', reason: 'forbidden
This actually means that the currently authenticated user (the service account) does ot have access to do what you are asking it to do.
You are trying to access a spread sheet.
Is this sheet on the service accounts google drive account? If not did you share the sheet with the service account?
The service account is just like any other user if it doesn't have access to something it cant access it. Go to the google drive web application and share the sheet with the service account like you would share it with any other user just use the service account email address i think its called client id its the one with an # in it.
delegate to user on your domain
If you set up delegation properly then you can have the service account act as a user on your domain that does have access to the file.
delegated_credentials = credentials.with_subject('userWithAccess#YourDomain.org')

How set API KEY in Google Translate Node.js code

I'm trying to create a Node.js code that uses google translate api.
I got the code below from the google doc (https://cloud.google.com/translate/docs/translating-text)
But when I run it, it says "Error: The request is missing a valid API key."
I have the key, but i don't know how and where to set it.
async function translate() { // Imports the Google Cloud client library
const { Translate } = require('#google-cloud/translate');
// Creates a client
const translate = new Translate();
/**
* TODO(developer): Uncomment the following lines before running the sample.
*/
const text = 'Hello, world!';
const target = 'ru';
// Translates the text into the target language. "text" can be a string for
// translating a single piece of text, or an array of strings for translating
// multiple texts.
let [translations] = await translate.translate(text, target);
translations = Array.isArray(translations) ? translations : [translations];
console.log('Translations:');
translations.forEach((translation, i) => {
console.log(`${text[i]} => (${target}) ${translation}`);
});
}
translate()
This page on setting up authentication explains that you need to download a credentials file from the create service account key page. This can then be added to your path (.bashrc) as follows:
export GOOGLE_APPLICATION_CREDENTIALS="[PATH]"
Alternately, you could add the line above to a .env file on your project root and source it when you are running the application:
. ./.env
npm start
or
sh -ac '. ./.env; npm start'
Checkout this Google Authentication Page to add the key
In the GCP Console, go to the Create service account key page.
From the Service account list, select New service account.
In the Service account name field, enter a name.
From the Role list, select Project > Owner. Click
Create. A JSON file that contains your key downloads to your computer.
and
export GOOGLE_APPLICATION_CREDENTIALS="[PATH to key downloaded]"
create api-key see documentation create api key doc
use it like:
import { v2 } from '#google-cloud/translate';
const translateClint = new v2.Translate({
projectId:'your-projectId-here',
key: 'your-api-key-here',
});
I don't check it for v3, but I see the same interface:
new v3.TranslationServiceClient({
key:"may be works",
projectId:"may be works"
})
Try this... no environment variables AND please... add this file to your .gitignore
import * as credentials from 'credentials.json';
...
const {Translate} = require('#google-cloud/translate').v2;
const translationApi = new Translate({
projectId:'your-project-id',
credentials:credentials
});

Resources