Azure Insert failed in database but query works - azure

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.

Related

How to properly send a GraphQL update request using Axios?

I created an application using AWS Amplify, and I want to create an endpoint in an API function that will mutate a field in GraphQL. Since its a nodejs API function, I cannot use the recommended libraries from the AWS documentation which use ES6, since the functions can only use ES5. Therefore I need to use Axios.
I created a graphql query:
const query = /* GraphQL */ `mutation updatePublication($id: ID!, $keywords: String) {
updatePublication(id: $id, keywords: $keywords){
id
keywords
}
}`
Next, I created the Axios request based on this StackOverflow question.
const data = await axios.post(
process.env.API_APPNAME_GRAPHQLAPIENDPOINTOUTPUT,
{
query: query,
variables: {
id: variableWithID,
keywords: "updated keywords!"
}
},
{
headers: {
'Content-Type': 'application/json',
'x-api-key': process.env.API_APPNAME_GRAPHQLAPIKEYOUTPUT
}
}
)
When I run the query, I get a status 200 from the server with the following errors:
data: null,
errors: [
{
path: null,
locations: [Array],
message: "Validation error of type MissingFieldArgument: Missing field argument input # 'updatePublication'"
},
{
path: null,
locations: [Array],
message: "Validation error of type UnknownArgument: Unknown field argument id # 'updatePublication'"
},
{
path: null,
locations: [Array],
message: "Validation error of type UnknownArgument: Unknown field argument keywords # 'updatePublication'"
}
]
}
Can anyone advise on what this means?

Elastic Search Node.js client - Routing Error

We have elastic hosted on GCP. I'm getting an error when I try to simply post a document. I'm using their node.js package, and I'm stuck with the following error:
PS C:\Projects\foo> node --experimental-modules --unhandled-rejections=strict app.js
(node:19356) ExperimentalWarning: The ESM module loader is experimental.
(node:19356) ExperimentalWarning: Conditional exports is an experimental feature. This feature could change at any time
Posting to Elastic => 5f91dfb0c64004000aab6c9b
C:\Projects\foo\node_modules\#elastic\elasticsearch\lib\Transport.js:257
const error = new ResponseError(result)
^
ResponseError: Response Error
at IncomingMessage.<anonymous> (C:\Projects\foo\node_modules\#elastic\elasticsearch\lib\Transport.js:257:25)
at IncomingMessage.emit (events.js:323:22)
at endReadableNT (_stream_readable.js:1204:12)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
name: 'ResponseError',
meta: {
body: { error: 'Routing Error. The path you have requested is invalid.' },
statusCode: 404,
headers: {
'content-type': 'application/json;charset=utf-8',
server: 'Jetty(9.4.30.v20200611)',
'x-cloud-request-id': 'srCq9DMFQVWuGsdXxgWhjA',
'x-content-type-options': 'nosniff',
'x-found-handling-cluster': '[Removed]',
'x-found-handling-instance': 'instance-0000000001',
'x-frame-options': 'SAMEORIGIN',
'x-request-id': 'a2696b33-e4dc-4dfc-a8b1-f7ec17906ff7',
'x-runtime': '0.010689',
'x-xss-protection': '1; mode=block',
date: 'Mon, 09 Nov 2020 21:28:28 GMT',
'content-length': '66'
},
meta: {
context: null,
request: [Object],
name: 'elasticsearch-js',
connection: [Object],
attempts: 0,
aborted: false
}
}
}
I have the client set up as follows
const client = new Client({
node: 'https://[REMOVED].ent-search.us-central1.gcp.cloud.es.io',
auth: {
apiKey: 'Bearer private-[REMOVED]'
}
});
Then I attempt to send a json file as an array to Elastic.
client.helpers.bulk({
datasource: posts,
onDocument(doc) {
return {
create: {_index: 'my-index', _id: elasticPost.id}
};
}
});
Update:
The JSON names MUST be lowercase. For example:
Doesn't Work:
{
"Id": 123456
}
Works:
{
"id": 123456
}
Another thing may have to do with needing the cloud ID from Elastic, I'm not sure if this is required as well, but the lowercase allowed me to upload our document directly into Elastic.
I think you have one too many create in the command you return from onDocument. The command you can return is described here:
So, by removing one create layer, it should work:
client.helpers.bulk({
datasource: posts,
onDocument(doc) {
return {
create: {_index: 'my-index', _id: elasticPost.id}
};
}
});
UPDATE:
Looking at the host name *.ent-search.*, it looks like you're using the elasticsearch client to try to connect to the Enterprise search backend, that cannot work as the latter doesn't support the _bulk API.
If you connect to an AppSearch backend, you can't use the the _bulk endpoint (i.e. client.helpers.bulk()). Instead, you need to add documents through the AppSearch Documents API. Feel free to share more info on what you're trying to achieve.
The JSON names MUST be lowercase. For example:
Doesn't Work:
{
"Id": 123456,
"userName": "Dave2118"
}
Works:
{
"id": 123456,
"username": "Dave2118"
}

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

problem of unauthorized in IBM watson assistant Nodejs

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.

CouchDB throws '404 missing' error when inserting a user using nano

It seems CouchDB will randomly throw an error when trying to insert a user.
Here is the full response from CouchDB:
{ [Error: missing]
name: 'Error',
scope: 'couch',
status_code: 404,
'status-code': 404,
request:
{ method: 'POST',
headers:
{ 'content-type': 'application/json',
accept: 'application/json',
'X-CouchDB-WWW-Authenticate': 'Cookie',
cookie: [Object] },
uri: 'http://127.0.0.1:5984/_users',
body: '{"_id":"org.couchdb.user:fake_user","name":"fake_user","realname":"fake user","institution":"366a8e5ba861bdd9cad5cd318a002ee4","email":"fake_user#gmail.com","phone":"123-456-7890","type":"user","roles":[],"password":"123456","level":"user","unconfirmed":"true"}',
jar: false },
headers:
{ date: 'Tue, 21 Jan 2014 20:32:23 GMT',
'content-type': 'application/json',
'cache-control': 'must-revalidate',
'status-code': 404,
uri: 'http://127.0.0.1:5984/_users' },
errid: 'non_200',
error: 'not_found',
reason: 'missing',
description: 'missing',
stacktrace:
[ 'Error: missing',
' at Request._callback (/apps/arcapp/node_modules/nano/nano.js:304:39)',
' at Request.self.callback (/apps/arcapp/node_modules/nano/node_modules/request/request.js:129:22)',
' at Request.EventEmitter.emit (events.js:98:17)',
' at Request.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:873:14)',
' at Request.EventEmitter.emit (events.js:117:20)',
' at IncomingMessage.<anonymous> (/apps/arcapp/node_modules/nano/node_modules/request/request.js:824:12)',
' at IncomingMessage.EventEmitter.emit (events.js:117:20)',
' at _stream_readable.js:872:14',
' at process._tickCallback (node.js:415:13)' ] }
This is obviously not very insightful. All I get is error: not_found, reason: missing, which doesn't tell me much. It only happens sometimes and I can't really tell what's triggering it. Any insight into this is much appreciated.
Here is the relevant code. I'm using nano, but it's just a basic post request to CouchDB.
var user = {
"_id": "org.couchdb.user:" + username,
"name": username,
"realname": first + " " + last,
"institution": institution,
"email": email,
"phone": phone,
"type": "user",
"roles": [],
"password": password,
"level": "user",
"unconfirmed": "true",
"verificationCode": verificationCode
};
nano.request({
db: '_users',
method: 'POST',
body: user
}, function(err, body) {
if(err) {
console.log("ERROR INSERTING USER");
console.log(err);
res.send(err.status_code + " " + err.reason, err.status_code);
return;
}
...
The problem here was that the cookie I was using to validate a user was not being properly unset after logout. So if you logged out and immediately tried to register, the database would authenticate the request as the logged in user. Since only db administrators are allowed access to the _users db, CouchDB would throw a '404 missing' error.
I was setting nano.config.cookie on login, so when I unset it on logout, it worked!
This error rings a bell but unfortunately I'm not 100% how/if I fixed it.
One thing I did notice you are including "roles:[]" I dont think this is allowed unless your post as an admin.
I haven't enough rep to comment but can you send the code that's making the request ?

Resources