Watson API on Node.js: error invalid text representation - node.js

I'm using Watson API on my Node.js application. I successfully connected my application with the API, but I when I run my app I got this message:
"error invalid text representation"
This is my code :
var watson = require('watson-developer-cloud');
var conversation = watson.conversation({
url: 'https://gateway.watsonplatform.net/conversation-experimental/api',
username: '*********',
password: '*******',
version: 'v1',
version_date: '2016-07-01'
});
// req.body.text
conversation.message({
input: 'what is your name',
workspace_id: '***'
}, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(response, null, 2));
}
});

your URL and version seems to be wrong..
Try this:
conversation.message({
input: {
text: 'what is your name'
},
workspace_id: '***'
}, function(err, response) {
if (err) {
console.error(err);
} else {
console.log(JSON.stringify(response, null, 2));
}
});
You have to add a 'text' to your JSON

Related

Using the google API Npm package I want to upload a video on youtube but the video file gets to from the request so how to do that?

I use node js rest APIs, below the code, I tried to pass buffer, but it is not working here is my code:-
service.videos.insert(
{
auth: oauth2Client,
part: "snippet,status",
requestBody: {
snippet: {
title: "Xyz2",
description: "abc",
categoryId: 28,
defaultAudioLanguage: "en",
defaultLanguage: "en",
},
status: {
privacyStatus: "public",
},
},
media: {
mimeType:"video/*",
body: Buffer.from(req.files.video),
},
},
(err, response) => {
console.log('response: ', response);
if (err) {
console.log(err);
}
// fs.unlink(`${req.files.video.name.replace(/ /g, '')}`)
var subscribers = response.data.items;
console.log(subscribers);
res.send({ subscribers });
}
);
it's return response successfully but when I go to the youtube channel it shows the error here is the error screenshot

Post request sends undefined data inn flutter app

I was working on a simple app with login functionality , but im unable to send username and password properly to nodejs server. I have tried encoding it, putting it as Map and FormData, but nothing seems to workout. I console logged the request body and it prints "undefind"
I'm using Dio dart package for making http requests and Redux and redux thunk to dispatch actions .
//Code on My flutter app
ThunkAction<AppState> login(LoginData data) {
return (Store<AppState> store) async {
store.dispatch(IsLoading(true));
try {
Response response = await Dio().post(
"http://10.0.2.2:4000/api/user/login",
data: json.encode({"phone": data.phone, "password": data.password}));
if (response.statusCode == 200) {
print(json.decode(response.data));
store.dispatch(IsLoading(false));
}
} catch (e) {
print("Error :(");
}
};
}
// Code on My nodejs
router.post("/login", (req, res) => {
//this log prints undefined
console.log("Login route: " + req.body.phone);
var cred = {
phone: req.body.phone,
password: req.body.password
};
User.findOne({ phone: cred.phone })
.then(result => {
if (!result) {
res.status(400).json({ msg: "no user" });
} else {
bcrypt.compare(req.body.password, result.password, (err, isMatch) => {
if (isMatch) {
const payload = { id: result._id };
console.log("Logged in :" + payload);
jwt.sign(
payload,
keys.secretOrKey,
{ expiresIn: 7200 },
(err, token) => {
res.status(200).json({
success: true,
token: "Bearer " + token
});
}
);
} else {
res.status(400).json({ msg: err });
}
});
}
})
.catch(err => {
res.status(400).json({ msg: err });
});
});
To access parameter in server side add this header to you request:
HttpHeaders.contentTypeHeader: 'application/x-www-form-urlencoded'

auto confirm users Cognito + Node JS

I trying to auto confirm my users in cognito + node Js app.
I tried this code as a Util, and here inside the function; and cant make it work
I try to go to aws documentation but the code is ambiguous and doesn't explain to much.
here is my code:
userPool.signUp(req.body.email, req.body.password, attributeList,
null, function (err, result) {
event = {
request: {
"userAttributes": {
"email": req.body.email
},
"validationData": {
"Name": "email",
"Value": req.body.email
}
},
response: {
autoVerifyEmail: true
}
}
// Confirm the user
// Set the email as verified if it is in the request
if (event.request.userAttributes.hasOwnProperty("email")) {
event.response.autoVerifyEmail = 'true';
}
// Return to Amazon Cognito
callback(null, event);
if (err) {
console.log("Error aws: ", err.message);
// return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
next();
// return;
});
}
can anyone help me to say, what Im doing wrong?
thanks
in the signup callback you should call AdminConfirmSignUp. the syntax is given below.
https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminConfirmSignUp.html
userPool.signUp(req.body.email, req.body.password, attributeList,
null, function (err, result) {
event = {
request: {
"userAttributes": {
"email": req.body.email
},
"validationData": {
"Name": "email",
"Value": req.body.email
}
},
response: {
autoVerifyEmail: true
}
}
// Confirm the user
var confirmParams = {
UserPoolId: 'provide your user pool id', /* required */
Username: req.body.email /* required */
};
cognitoidentityserviceprovider.adminConfirmSignUp(confirmParams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
// Set the email as verified if it is in the request
if (event.request.userAttributes.hasOwnProperty("email")) {
event.response.autoVerifyEmail = 'true';
}
// Return to Amazon Cognito
callback(null, event);
if (err) {
console.log("Error aws: ", err.message);
// return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
next();
// return;
});
});
}

NodeJS Google Calendar API returns 'Missing scope type' error

When I make this call using the google api for nodejs:
var google = require('googleapis');
var calendar = google.calendar('v3');
calendar.acl.insert({
calendarId : "exampleCalID#gmail.com",
auth: auth.client,
role: "reader",
scope: {
type: "user",
value: "exampleUser#gmail.com"
}
}, function(err, response){
if(err)
console.log(err)
console.log(response);
return;
});
I get the following error from the google API:
{ [Error: Missing scope type.]
code: 400,
errors:
[ { domain: 'global',
reason: 'required',
message: 'Missing scope type.' } ] }
I figured it out. The role and scope data of this call need to be in an object called resource. Line 128 of the source code makes this more clear than the documentation, which is not language specific. So the function call should look like this:
var google = require('googleapis');
var calendar = google.calendar('v3');
calendar.acl.insert({
calendarId : "exampleCalID#gmail.com",
auth: auth.client,
resource:{
role: "reader",
scope: {
type: "user",
value: "exampleUser#gmail.com"
}
}
}, function(err, response){
if(err)
console.log(err)
console.log(response);
return;
});

Sending Message using facebook Graph API

I'm trying to send a message to a facebook user, using POST method :
Here is the function :
router.post('/post/message/:userid', function (req, res, next) {
messageData = {
recipient: {id: req.params.userid},
message: {text:req.body.text}
}
console.log( messageData );
request({
url: 'https://graph.facebook.com/v2.6/'+ pageid +'/messages',
qs: {access_token: token_raw},
method: 'POST',
json: messageData
}, function (error, response, body) {
if (error) {
console.log('Error sending message: ', error);
} else if (response.body.error) {
console.log('Error: ', response.body.error);
}
});
});
But then here are the console logs :
{ recipient: { id: '**ClientID**' },
message: { text: 'hello' } }
type: 'OAuthException',
Error: { message: '(#100) No matching user found',
code: 100,
fbtrace_id: 'FlDy9Ypfen8' }
While "ClientID" is actually a valid userID.
Any idea ?

Resources