torreyleonard / algotrader

Simple algorithmic stock and option trading for Node.js.
https://www.npmjs.com/package/algotrader
Apache License 2.0
637 stars 120 forks source link

Error: Robinhood responded with code 401 #11

Closed Gillinghammer closed 5 years ago

Gillinghammer commented 5 years ago

Running into an issue that happened recently, so I am wondering if something changed in the Robinhood API.

const instrument = await Instrument.getBySymbol(symbol);
// works fine & returns the instrument from Robinhood
// But now when I use that generated instrument to get the Quote I have a 401 response from Robinhood
const stockQuote = await instrument.getQuote();
// Error: Robinhood responded with code 401
trapadulli commented 5 years ago

I have the same issue. Robinhood must've updated. So, I downloaded Algotrader thinking it would resolve issue however I'm still having the same issue after npm'ing latest. Also, I don't understand how to use getMFA() if I decided to use it....

Gillinghammer commented 5 years ago

@trapadulli

So this is kinda hacky but I got things working by editing the file at node_modules/algotrader/objects/broker/robinhood/Instrument.js

Edit the request to include an Authorization Header like so.

Now when you call instrument.getQuote() you must pass in your authenticated user like: instrument.getQuote(user).

getQuote(user) {
    const _this = this;
    return new Promise((resolve, reject) => {
      request(
        {
          uri: _this.urls.quote,
          headers: {
            Authorization: 'Bearer ' + user.getAuthToken()
          })
....

@Ladinn Pretty hacky like I said, but let me know if you'd like me to do a pull request for this.

trapadulli commented 5 years ago

@Gillinghammer thanks! I look forward to trying this tonight.

torreyleonard commented 5 years ago

@Gillinghammer Thanks for figuring this out!

I just updated the readme, docs, and Instrument class' getQuote and populate methods to reflect the change. After testing without errors this issue is resolved.

trapadulli commented 5 years ago

@Ladinn _preAuth rejects body: "Unable to log in with provided credentials."... but my User object has a token. Why?

torreyleonard commented 5 years ago

@trapadulli can you post the version you're using, the full error, and the code that causes the error?

trapadulli commented 5 years ago

@trapadulli can you post the version you're using, the full error, and the code that causes the error? on algotrader@1.4.3 I can't get past authenticate

user.authenticate()
.then(() => {
console.log("never gets here............")
})
.catch(error => {
console.log(error)
})

Error: "Unable to log in with provided credentials."