problem of unauthorized in IBM watson assistant Nodejs - node.js

I tried the API Nodejs v1 and v2 of IBM Watson assistant to creat a new conversion. But it gived me always the same error code 401 Unauthorized: Access is denied due to invalid credentials. i don't know what happened, thanks in advance for your answers.
{ Unauthorized: Access is denied due to invalid credentials.
at RequestWrapper.formatError (E:\Documents\Techniques\Desktop\front_back_end\version-watson\node_modules\ibm-watson\node_modules\ibm-cloud-sdk-core\lib\requestwrapper.js:218:21)
at E:\Documents\Techniques\Desktop\front_back_end\version-watson\node_modules\ibm-watson\node_modules\ibm-cloud-sdk-core\lib\requestwrapper.js:206:29
at process._tickCallback (internal/process/next_tick.js:68:7)
name: 'Unauthorized',
code: 401,
message: 'Access is denied due to invalid credentials.',
body: '{"code":401,"error":"Unauthorized"}',
headers:
{ 'x-backside-transport': 'FAIL FAIL',
'content-type': 'application/json',
'x-dp-transit-id': 'gateway01-1474836587',
'x-global-transaction-id': '7ecac92c5d14b5ae57e8386b',
'strict-transport-security': 'max-age=31536000;',
'x-dp-watson-tran-id': 'gateway01-1474836587',
'content-length': '37',
'x-edgeconnect-midmile-rtt': '82',
'x-edgeconnect-origin-mex-latency': '110',
date: 'Thu, 27 Jun 2019 12:25:18 GMT',
connection: 'close' } }
the document API of IBM assistant shows me the code like:
const AssistantV1 = require('ibm-watson/assistant/v1');
const service = new AssistantV1({
version: '2019-02-28',
iam_apikey: '{apikey}',
url: '{url}'
});
service.message({
workspace_id: '{workspace_id}',
input: {'text': 'Hello'}
})
.then(res => {
console.log(JSON.stringify(res, null, 2));
})
.catch(err => {
console.log(err)
});
I am not sure that for Version2 the session-id is correct or not. But the API v1 doesn't need the session_id, it gives the same error code too.
I want to comfirm that 'Assistant Settings', 'API details', we can find the information of keys.
And'{apikey}' is 'Service Credentials--->passeword', and '{workspace_id}' is 'Assistant Details ---> Assistant ID '. What i confirm is correct, right?

On V1 all credentials information's can be get through "View API Details" on your Skill.
The apikey are on the item Service Credential/Password (the username will be apikey). The workspace ID are also on this interface.
The assistant is used only with the v2, where the session ID is given to you during the first message of the conversation.

Related

Problems connecting Service Account to Admob API with Google-Auth-Library

I've been trying to connect to Admob API from an AWS Lambda to extract some values from reports automatically from time to time. I've successfully got google-auth-library-nodejs to a layer and I am trying to use it to connect to Admob API.
I've made sure to give my Service account an Owner role and I've added the necessary GOOGLE_APPLICATION_CREDENTIALS path to the environement variables.
This is the code that I've added to my Lambda:
const {GoogleAuth} = require('google-auth-library');
exports.handler = (event, context, callback) => {
async function main() {
const auth = new GoogleAuth({
scopes: ['https://www.googleapis.com/auth/admob.report'],
});
const client = await auth.getClient();
//console.log("client", JSON.stringify(client));
const url = `https://admob.googleapis.com/v1/accounts`;
const res = await client.request({ url });
console.log("res: ", JSON.stringify(res.data));
}
main().catch(console.error);
};
When I run the code, I get the following error:
ERROR GaxiosError: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.
at Gaxios._request (/opt/nodejs/node_modules/gaxios/build/src/gaxios.js:85:23)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at async JWT.requestAsync (/opt/nodejs/node_modules/google-auth-library/build/src/auth/oauth2client.js:350:18)
at async main (/var/task/index.js:97:19) {
response: {
config: {
url: 'https://admob.googleapis.com/v1/accounts',
headers: [Object],
params: [Object: null prototype] {},
paramsSerializer: [Function: paramsSerializer],
validateStatus: [Function: validateStatus],
responseType: 'json',
method: 'GET'
},
data: { error: [Object] },
headers: {
'alt-svc': 'quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000',
'cache-control': 'private',
connection: 'close',
'content-encoding': 'gzip',
'content-type': 'application/json; charset=UTF-8',
date: 'Wed, 26 Feb 2020 18:41:51 GMT',
server: 'ESF',
'transfer-encoding': 'chunked',
vary: 'Origin, X-Origin, Referer',
'x-content-type-options': 'nosniff',
'x-frame-options': 'SAMEORIGIN',
'x-xss-protection': '0'
},
status: 401,
statusText: 'Unauthorized',
request: { responseURL: 'https://admob.googleapis.com/v1/accounts' }
},
config: {
url: 'https://admob.googleapis.com/v1/accounts',
headers: {
Authorization: 'Bearer [Removed]',
'User-Agent': 'google-api-nodejs-client/5.10.1',
'x-goog-api-client': 'gl-node/12.14.1 auth/5.10.1',
Accept: 'application/json'
},
params: [Object: null prototype] {},
paramsSerializer: [Function: paramsSerializer],
validateStatus: [Function: validateStatus],
responseType: 'json',
method: 'GET'
},
code: 401,
errors: [
{
message: 'Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.',
domain: 'global',
reason: 'unauthorized'
}
]
}
I've been trying to find my mistake by searching on google, stack overflow, reading tutorials about connecting to Admob API as well as reading the library's code. I would really appreciate if someone could point me towards a solution.
The problem is you are trying to use a service account where OAuth User Credentials are required. You will need to implement the OAuth 2 Flow where the user enters their Google username and password.
AdMob: Authorization for the request
Refer to the following Google example on how to create a node.js OAuth 2 client.
https://github.com/googleapis/google-auth-library-nodejs#oauth2

I have followed the api documentation for Watson Tone analyser to no avail - can't connect the two endpoints

I have a Watson Tone cloud service up and running and I am trying to link it to my index.js file. However it doesn't seem to want to connect.
Code below:
const ToneAnalyzerV3 = require('watson-developer-cloud/tone-analyzer/v3');
const toneAnalyzer = new ToneAnalyzerV3({
version: '2017-09-21',
iam_apikey: 'omoNirDx1MUQwCdlHTQ-5Fxklx2_c0dUVAyMFIItFLty',
url: 'http://watson-row-analyser.eu-gb.cf.appdomain.cloud/#'
});
const text =
'Team, I know that times are tough! Product ' +
'sales have been disappointing for the past three ' +
'quarters. We have a competitive product, but we ' +
'need to do a better job of selling it!';
const toneParams = {
tone_input: { text: text },
content_type: 'application/json'
};
toneAnalyzer
.tone(toneParams)
.then((toneAnalysis) => {
console.log(JSON.stringify(toneAnalysis, null, 2));
})
.catch((err) => {
console.log('error:', err);
});
Current error code:
Marcs-MacBook-Pro:npm-global marcwatts$ node index1.js
error: { Not Found: Not Found
at formatError (/Users/marcwatts/Desktop/mxw817/DBTestApp/npm-global/node_modules/watson-developer-cloud/node_modules/ibm-cloud-sdk-core/lib/requestwrapper.js:111:17)
at /Users/marcwatts/Desktop/mxw817/DBTestApp/npm-global/node_modules/watson-developer-cloud/node_modules/ibm-cloud-sdk-core/lib/requestwrapper.js:259:19
at process._tickCallback (internal/process/next_tick.js:68:7)
name: 'Not Found',
code: 404,
message: 'Not Found',
body:
'"<!DOCTYPE html>\\n<html lang=\\"en\\">\\n<head>\\n<meta charset=\\"utf-8\\">\\n<title>Error</title>\\n</head>\\n<body>\\n<pre>Cannot POST /</pre>\\n</body>\\n</html>\\n"',
headers:
{ 'x-backside-transport': 'FAIL FAIL',
connection: 'close',
'transfer-encoding': 'chunked',
'content-security-policy': 'default-src \'none\'',
'content-type': 'text/html; charset=utf-8',
date: 'Sat, 06 Jul 2019 15:32:34 GMT',
'x-content-type-options': 'nosniff',
'x-powered-by': 'Express',
'x-global-transaction-id': 'ba9308f85d20bf12a70673ff' } }
Link to api for Watson:
https://cloud.ibm.com/apidocs/tone-analyzer?code=node
Code 404 means Not Found. A requested item or parameter does not exist. What am I missing?

the problem of unauthorized in IBM watson assistant Nodejs

I tried the API v1 and v2 of IBM Watson assistant to creat a new conversion.
But it gived me always the samr error code 401 Unauthorized: Access is denied due to invalid credentials.
i don't know what happened, thanks in advance for your answers.
{ Unauthorized: Access is denied due to invalid credentials.
at RequestWrapper.formatError (E:\Documents\Techniques\Desktop\front_back_end\version-watson\node_modules\ibm-watson\node_modules\ibm-cloud-sdk-core\lib\requestwrapper.js:218:21)
at E:\Documents\Techniques\Desktop\front_back_end\version-watson\node_modules\ibm-watson\node_modules\ibm-cloud-sdk-core\lib\requestwrapper.js:206:29
at process._tickCallback (internal/process/next_tick.js:68:7)
name: 'Unauthorized',
code: 401,
message: 'Access is denied due to invalid credentials.',
body: '{"code":401,"error":"Unauthorized"}',
headers:
{ 'x-backside-transport': 'FAIL FAIL',
'content-type': 'application/json',
'x-dp-transit-id': 'gateway01-1474836587',
'x-global-transaction-id': '7ecac92c5d14b5ae57e8386b',
'strict-transport-security': 'max-age=31536000;',
'x-dp-watson-tran-id': 'gateway01-1474836587',
'content-length': '37',
'x-edgeconnect-midmile-rtt': '82',
'x-edgeconnect-origin-mex-latency': '110',
date: 'Thu, 27 Jun 2019 12:25:18 GMT',
connection: 'close' } }
const AssistantV1 = require('ibm-watson/assistant/v1');
const service = new AssistantV1({
version: '2019-02-28',
iam_apikey: '{apikey}',
url: '{url}'
});
service.message({
workspace_id: '{workspace_id}',
input: {'text': 'Hello'}
})
.then(res => {
console.log(JSON.stringify(res, null, 2));
})
.catch(err => {
console.log(err)
});
I am not sur that for Version2 the session-id is correct or not. But the API v1 doesn't need the session_id, it gives the same error code too.

Get GitHub branch with NodeJS with a repo logged with OAuth

I would get branches in a GitHub repo but with an OAuth authentication.
I use octonode (but you use another dep, no problem), so i thought get repo like that:
const github = require('octonode');
const client = github.client();
const repo = client.repo('https://x-oauth-basic:490b285b8f92a63abfc381aa1f7f5c40aa2f9274#github.com/my-username/my-repo.git');
But the result is:
{ [Error: Not Found]
message: 'Not Found',
statusCode: 404,
headers:
{ server: 'GitHub.com',
date: 'Wed, 03 Oct 2018 08:37:07 GMT',
'content-type': 'application/json; charset=utf-8',
'content-length': '77',
connection: 'close',
status: '404 Not Found', ... },
body:
{ message: 'Not Found',
documentation_url: 'https://developer.github.com/v3' } }
Indeed, the goad is to get branches of the repo.
I found the solution by using the module #octokit/rest.

Azure Insert failed in database but query works

I am trying to do an Insert/replace :
insertOrReplaceEntity('myusertables', task, function(error)
it always goes to error code indicating that insertion did not happen.
How to debug this in Azure?
I am using the Azure emulator and have in the code:
var account = azure.ServiceClient.DEVSTORE_STORAGE_ACCOUNT;
var accountKey = azure.ServiceClient.DEVSTORE_STORAGE_ACCESS_KEY;
I get PUT failed with 403.
{ error:
{ [Error: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctl
including the signature.]
code: 'AuthenticationFailed',
message: 'Server failed to authenticate the request. Make sure the value of Authorization header is formed correc
y including the signature.' },
response:
{ isSuccessful: false,
statusCode: 403,
body:
{ '#': [Object],
code: 'AuthenticationFailed',
message: [Object] },
headers:
{ 'content-length': '356',
'content-type': 'application/xml',
server: 'Microsoft-HTTPAPI/2.0',
date: 'Mon, 12 Nov 2012 20:57:10 GMT' },
md5: undefined } }
As Described here the storage emulator does not support Insert-Or-Replace Entity or Insert-Or-Merge Entity, known as upsert features. That's why when you use insertOrReplaceEntity in your code it return an error. If you have to verify the code, you may need to check it with the real Azure Table Storage.

Resources