mikepaszkiewicz/nanode

Creating api without apiKey and no authorization fails

Closed this issue · 13 comments

fu5ha commented

For example:

const Nano = require('nanode')
const nano = new Nano({url: "http://127.0.0.1:7076"});

nano.available().then((result) => {
    console.log(result);
}).catch((err) => {
    console.log(err);
});

Fails with:

Error: "value" required in setHeader("Authorization", value)
    at validateHeader (_http_outgoing.js:492:11)
    at ClientRequest.setHeader (_http_outgoing.js:501:3)
    at new ClientRequest (_http_client.js:173:14)
    at Object.request (http.js:38:10)
    at RedirectableRequest._performRequest (C:\Users\Gray\Code\nanodetest\node_modules\follow-redirects\index.js:128:24)
    at new RedirectableRequest (C:\Users\Gray\Code\nanodetest\node_modules\follow-redirects\index.js:54:8)
    at Object.wrappedProtocol.request (C:\Users\Gray\Code\nanodetest\node_modules\follow-redirects\index.js:252:14)
    at dispatchHttpRequest (C:\Users\Gray\Code\nanodetest\node_modules\axios\lib\adapters\http.js:131:25)
    at new Promise (<anonymous>)
    at httpAdapter (C:\Users\Gray\Code\nanodetest\node_modules\axios\lib\adapters\http.js:18:10)

You can solve this by passing fake authorization details in the url:

const nano = new Nano({url: "http://u:p@127.0.0.1:7076"});

I am also getting this error except i have my apikey in.
(node:9139) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: "value" required in setHeader("Authorization", value)

@Ankonian1 hmmm..can you provide an example for your initialization code?

Yeah, not sure what im doing wrong.
`var Nano = require("nanode")
const nano = new Nano({apikey: process.env.blah-blah-blah-blah-blah})
const nanoAccount = nano.account("blah")

console.log(nanoAccount.nanoBalance())`
getting syntax errors if i try this method. Have messed around with the api key part a lot and it just doesnt want to take it. Possibly cause there are "-" dashes in it?

fu5ha commented

Are you using literally process.env.blah-blah-blah or are you using process.env.API_KEY and then setting the environment variable API_KEY to blah-blah-blah? Variable names can't have - in them, but you shouldn't need it... if you do process.env.API_KEY then run like:

$ API_KEY=blah-blah-blah node script.js

That should work...

Also noticed in your example, the key name is apikey, whereas correct usage is apiKey.

Yeah, I tried both ways. Still getting
(node:12858) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): Error: "value" required in setHeader("Authorization", value)

if you are on version 2.1.0, run npm install nanode@latest, and make sure the version is 2.1.1. PR #14 should have fixed this. This should work:

//in script.js
const nano = new Nano({
   apiKey: process.env.API_KEY
})

then run:
$ API_KEY=your-api-key node script.js

Now getting.
(node:13197) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Request failed with status code 401

I see 401 is invalid api key but I know its correct as I copied it direct from the webpage.

maybe the API_KEY variable isn't getting set - does running this log your key to the console correctly? make sure there aren't quotes around the environment variable.

console.log(process.env.API_KEY)
const nano = new Nano({
   apiKey: process.env.API_KEY
})

Prints out my key just fine. but with that, im not getting any error and im not getting my balance printed. just get Promise { <pending> }

fu5ha commented

You're not using the promise. Most api calls return promises rather than the value itself.

nanoAccount.nanoBalance().then(balance => {
    console.log(balance)
})
fu5ha commented