Mathieu2301 / TradingView-API

📈 Get real-time stocks from TradingView
1.31k stars 307 forks source link

Credentials error: Wrong or expired sessionid #187

Closed 0xR-Dev closed 1 year ago

0xR-Dev commented 1 year ago

did the code still working?

im trying some example there but showing error

"Credentials error: Wrong or expired sessionid"

im have to put correct token id, but still get the error. anyone can help to solve it?

0xR-Dev commented 1 year ago

im try using this code

const TradingView = require("@mathieuc/tradingview");

if (!process.argv[2]) throw Error('Please specify your \'sessionid\' cookie');

const client = new TradingView.Client({ token: process.argv[2], });

const chart = new client.Session.Chart(); chart.setMarket('BINANCE:BTCEUR', { timeframe: 'D', });

TradingView.getPrivateIndicators(process.argv[2]).then((indicList) => { indicList.forEach(async (indic) => { const privateIndic = await indic.get(); console.log('Loading indicator', indic.name, '...');

const indicator = new chart.Study(privateIndic);

indicator.onReady(() => {
  console.log('Indicator', indic.name, 'loaded !');
});

indicator.onUpdate(() => {
  console.log('Plot values', indicator.periods);
  console.log('Strategy report', indicator.strategyReport);
});

}); });

output :

$ node indicatorTest.js Loading indicator EMA 20/50/100/200 ... Credentials error: Wrong or expired sessionid

daniil-dn commented 1 year ago

The same problem

hawooni commented 1 year ago

It seems that we also need to include the sessionid_sign.

hyjay commented 1 year ago

Same issue here. Seems like TradingView has made a breaking change.

hawooni commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

hyjay commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

+1. It works.

0xR-Dev commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

Thanks its, worked now..

kyr0 commented 1 year ago

Also, username/password based login still works well. At least with the most recent version via github


  "dependencies": {
    "@mathieuc/tradingview": "github:Mathieu2301/TradingView-API",
  }

Simply run await TradingView.loginUser(...) and then use the client without passing any arguments.

lordbritish888 commented 1 year ago

How can I use your method without it disconnecting my browser TradingView? @kyr0

I still get the same error with this code.. @0xR-Dev @hawooni

sign = "v1:j6C5uAzIpQcAfHIUMhyFESRrKWCeO/fv/mt+4qRRPsE="
  async getUser(session, location = 'https://www.tradingview.com/') {
    return new Promise((cb, err) => {
      https.get(location, {
        headers: { cookie: `sessionid=${session};sessionid_sign=${sign}` },
lordbritish888 commented 1 year ago

I got the sessionid and the sessionid_sign from the browser on an already logged in instance of TradingView

hawooni commented 1 year ago

I got the sessionid and the sessionid_sign from the browser on an already logged in instance of TradingView

If your current browser sessionId and sessionid_sign match, it should work just fine using both.

lordbritish888 commented 1 year ago

@hawooni it still isn't working :/

I even tried this:

const clientTV = new TradingView.Client({ token: process.argv[2], session: process.argv[2], sessionid_sign: "v1:j6C5uAzIpQcAfHIUMhyFESRrKWCeO/fv/mt+4qRRPsE=" });

lordbritish888 commented 1 year ago

@Mathieu2301 can you patch this please? or can anyone provide an updated miscRequests.js with corresponding login via token example?

hawooni commented 1 year ago

@hawooni it still isn't working :/

I even tried this:

const clientTV = new TradingView.Client({ token: process.argv[2], session: process.argv[2], sessionid_sign: "v1:j6C5uAzIpQcAfHIUMhyFESRrKWCeO/fv/mt+4qRRPsE=" });

Hard code the value sessionid_sign directly as mentioned above as this is just a temporary solution. Or you could try user/password method.

lordbritish888 commented 1 year ago

@hawooni The user/password method disconnects the browser session :/

are you using the vi: part or just the part after for signing?

hawooni commented 1 year ago

@hawooni The user/password method disconnects the browser session :/

are you using the vi: part or just the part after for signing?

I have not tried user/password method kyr0 mentioned that it is working.

lordbritish888 commented 1 year ago

@hawooni it works well, just you cannot use TV in your browser while its running and re-logging in on browser disconnects the NodeJS session :/

lordbritish888 commented 1 year ago

@0xR-Dev does your sign look like this?

sign = "v1:j6C5uAzIpQcAfHIUMhyFESRrKWCeO/fv/mt+4qRRPsE="

lordbritish888 commented 1 year ago

After the log in method was mostly good, I'm now getting this..

Login error: Please confirm that you are not a robot by clicking the captcha box .

amayp1 commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

I have replaced line 408 to like this but I still encounter the credentials error ?

    headers: { cookie: `sessionid=${session};sessionid_sign={v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=};` },
hawooni commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

I have replaced line 408 to like this but I still encounter the credentials error ?

    headers: { cookie: `sessionid=${session};sessionid_sign={v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=};` },

your value should be without {} => ...`sessionid=${session};sessionid_sign=v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=;`},

amayp1 commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

I have replaced line 408 to like this but I still encounter the credentials error ?

    headers: { cookie: `sessionid=${session};sessionid_sign={v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=};` },

your value should be without {} => ...sessionid=${session};sessionid_sign=v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=;},

I tried it but then it gives me this error. Do we nee do make changes in other files?

TypeError: str.replace is not a function at Object.parseWSPacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\protocol.js:20:16←[90m)←[39m at #parsePacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:160:14←[90m)←[39m at WebSocket. ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:262:55←[90m)←[39m ←[90m at WebSocket.emit (node:events:513:28)←[39m at Receiver.receiverOnMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\websocket.js:1184:20←[90m)←[39m ←[90m at Receiver.emit (node:events:513:28)←[39m at Receiver.dataMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:541:14←[90m)←[39m at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:492:23 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:309:9 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:392:7

Node.js v19.9.0

hawooni commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

I have replaced line 408 to like this but I still encounter the credentials error ?

    headers: { cookie: `sessionid=${session};sessionid_sign={v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=};` },

your value should be without {} => ...sessionid=${session};sessionid_sign=v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=;},

I tried it but then it gives me this error. Do we nee do make changes in other files?

TypeError: str.replace is not a function at Object.parseWSPacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\protocol.js:20:16←[90m)←[39m at #parsePacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:160:14←[90m)←[39m at WebSocket. ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:262:55←[90m)←[39m ←[90m at WebSocket.emit (node:events:513:28)←[39m at Receiver.receiverOnMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\websocket.js:1184:20←[90m)←[39m ←[90m at Receiver.emit (node:events:513:28)←[39m at Receiver.dataMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:541:14←[90m)←[39m at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:492:23 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:309:9 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:392:7

Node.js v19.9.0

try again with nodejs18

amayp1 commented 1 year ago

temporary, if you include "sessionid=${session};sessionid_sign={YOUR_VALUE};" in miscRequests.js line 408 should work.

I have replaced line 408 to like this but I still encounter the credentials error ?

    headers: { cookie: `sessionid=${session};sessionid_sign={v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=};` },

your value should be without {} => ...sessionid=${session};sessionid_sign=v1:Ms/DU43561uJEfjdkbJPrz1GDBNLrl6YTIpmDY=;},

I tried it but then it gives me this error. Do we nee do make changes in other files? TypeError: str.replace is not a function at Object.parseWSPacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\protocol.js:20:16←[90m)←[39m at #parsePacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:160:14←[90m)←[39m at WebSocket. ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:262:55←[90m)←[39m ←[90m at WebSocket.emit (node:events:513:28)←[39m at Receiver.receiverOnMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\websocket.js:1184:20←[90m)←[39m ←[90m at Receiver.emit (node:events:513:28)←[39m at Receiver.dataMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:541:14←[90m)←[39m at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:492:23 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:309:9 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:392:7 Node.js v19.9.0

try again with nodejs18

Still same error

C:\Users\Administrator\Desktop\STTradingstrat\TradingView-API-main\src\protocol.js:20 return str.replace(cleanerRgx, '').split(splitterRgx) ^

TypeError: str.replace is not a function at Object.parseWSPacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\protocol.js:20:16←[90m)←[39m at #parsePacket ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:160:14←[90m)←[39m at WebSocket. ←[90m(C:\Users\Administrator\←[39mDesktop\STTradingstrat\TradingView-API-main\src\client.js:262:55←[90m)←[39m ←[90m at WebSocket.emit (node:events:513:28)←[39m at Receiver.receiverOnMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\websocket.js:1184:20←[90m)←[39m ←[90m at Receiver.emit (node:events:513:28)←[39m at Receiver.dataMessage ←[90m(C:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:541:14←[90m)←[39m at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\receiver.js:492:23 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:309:9 at ←[90mC:\Users\Administrator\←[39mnode_modules\←[4mws←[24m\lib\permessage-deflate.js:392:7

Node.js v18.16.0

lordbritish888 commented 1 year ago

The login with user/pass (when it doesn't require a captcha which it sometimes does) is now working for basic chart. However, the private indicators will not load. This appears to have changed today?

amayp1 commented 1 year ago

Is anyone able to get private indicator data?

lordbritish888 commented 1 year ago

@amayp1 I am not able to right now. And none of the people who offered solutions have shown examples of the fix with the sessionid_sign but that seems to be the only way since credentials login is not serving the data.

I have tried everything suggested.

Mathieu2301 commented 1 year ago

Hi everyone! I can't reproduce the bug described in this issue. I have tested the getUser, getPrivateIndicators methods, and I have also tried to create a TradingView client:

const client = new TradingView.Client({
  token: process.env.SESSION,
});

then added the user's indicators one by one.

I didn't have any problems and didn't need to include sessionid_sign. I am using the latest version from GitHub.

I added a new automated test for these authenticated methods. I created a TradingView account for the occasion. You can find the code of this test here: https://github.com/Mathieu2301/TradingView-API/blob/main/tests/10.%20Authenticated.js You can find the results of the last tests here: https://github.com/Mathieu2301/TradingView-API/actions/runs/4773889220

I don't recommend abusing the loginUser method because it creates a new session every time it is called. Go to https://www.tradingview.com/u/YOUR_USERNAME/#security in the "PRIVACY AND SECURITY" tab to see the list of active sessions.

Can someone give me more information about the bug described in this issue? Thanks 😊

hawooni commented 1 year ago

Hi everyone! I can't reproduce the bug described in this issue. I have tested the getUser, getPrivateIndicators methods, and I have also tried to create a TradingView client:

const client = new TradingView.Client({
  token: process.env.SESSION,
});

then added the user's indicators one by one.

I didn't have any problems and didn't need to include sessionid_sign. I am using the latest version from GitHub.

I added a new automated test for these authenticated methods. I created a TradingView account for the occasion. You can find the code of this test here: https://github.com/Mathieu2301/TradingView-API/blob/main/tests/10.%20Authenticated.js You can find the results of the last tests here: https://github.com/Mathieu2301/TradingView-API/actions/runs/4773889220

I don't recommend abusing the loginUser method because it creates a new session every time it is called. Go to https://www.tradingview.com/u/YOUR_USERNAME/#security in the "PRIVACY AND SECURITY" tab to see the list of active sessions.

Can someone give me more information about the bug described in this issue? Thanks 😊

I tested it, and it appears that the sessionid_sign is no longer required. Thx :)

gasparesganga commented 1 year ago

I had started to work on a patch to include sessionid_sign in the library but indeed it seems it is no longer required. Such a strange and unpredictable behaviour from TV staff...

Anyways, is anyone experiencing another issue right now when trying to load more than 3 indicators/studies at once? I am getting this error message: The maximum number of studies per chart has been reached for current subscription It goes without saying that I am using a TradingView account that allows for more than 3 indicators per chart. The same code used to work just fine until last week when sessionid_sign was introduced and then dropped in order to resume a session.

I am not starting a new issue because I suspect it might be related to this one. Hopefully Mathieu will forgive me if it's not :)

msitarz commented 1 year ago

@gasparesganga Your issue with indicator limit might be due to TV simply using anonymous session rather than logged in because you didn't provide sessionid_sign properly. I had problem with using various timeframes from my pro account when I didn't provide sessionid_sign.

@Mathieu2301 can you check again if you are logged in properly without using the sign parameter and then using some pro account features like second timeframes?

I already created a patch but only on my local copy and I'm using version 3.3.3. I'll test with the newest version and I'll provide pull request when I have a bit of time so you guys can check.

gasparesganga commented 1 year ago

@msitarz This is not the case. As I wrote, it seems TV is now ignoring whether we provide a sessionid_sign or not. I am providing a valid sessionid_sign anyways, but this new issue does not depend on it. It happens even using just loginUser and no session at all. Are you able to get data for more than 3 indicators/studies at the same time?

@Mathieu2301 In the meanwhile I finished a patch to fully integrate sessionid_sign into v3.4.0. If it turns out it's still required, I can publish a rep and make a pull request. Or maybe I can add an option to turn this signature on and off? Your call!

msitarz commented 1 year ago

@gasparesganga I've checked using the new version and code without sessionid_sign and indeed it works correctly. I don't have the issues with timeframes that I had before. I don't use indicators so I cannot tell you if I can use more than 3.

It looks like tradingview tech team did some changes to their auth API previously and now they rolled it back.

gasparesganga commented 1 year ago

Alright, the error I was getting was due to a silly mistake in my code. Everything is working great again without any sessionid_sign. It does seem strange indeed that they added and quickly ditched it after a few days, but it is what is it. In case it came back, I got a patch ready for it.

sohaha commented 1 year ago

I would like to kindly inquire if anyone might be aware of the approximate duration before a session ID expires.

zwarrior1 commented 1 year ago

Hi everyone, I started writing a trading bot yesterday and I don't know whatever it is fixed or not, but made some changes just for temporary fix.

examples/CustomTimeframe.js:10 ~ 14

const client = new TradingView.Client({
  token: "",
  sessionid_sign: ""
});

src/client.js:232 ~ 234

if (clientOptions.token && clientOptions.sessionid_sign) {
      misc.getUser(clientOptions.token, clientOptions.sessionid_sign).then((user) => {

src/miscRequests.js:405 ~ 443

async getUser(sessionid, sessionid_sign='', location = 'https://www.tradingview.com/') {
    return new Promise((cb, err) => {
      https.get(location, {
        headers: { cookie: `sessionid=${sessionid}; sessionid_sign=${sessionid_sign}` },
      }, (res) => {
        let rs = '';
        res.on('data', (d) => { rs += d; });
        res.on('end', async () => {
          if (res.headers.location && location !== res.headers.location) {
            cb(await module.exports.getUser(sessionid, sessionid_sign, res.headers.location));
            return;
          }
          if (rs.includes('auth_token')) {
            cb({
              id: /"id":([0-9]{1,10}),/.exec(rs)[1],
              username: /"username":"(.*?)"/.exec(rs)[1],
              firstName: /"first_name":"(.*?)"/.exec(rs)[1],
              lastName: /"last_name":"(.*?)"/.exec(rs)[1],
              reputation: parseFloat(/"reputation":(.*?),/.exec(rs)[1] || 0),
              following: parseFloat(/,"following":([0-9]*?),/.exec(rs)[1] || 0),
              followers: parseFloat(/,"followers":([0-9]*?),/.exec(rs)[1] || 0),
              notifications: {
                following: parseFloat(/"notification_count":\{"following":([0-9]*),/.exec(rs)[1] || 0),
                user: parseFloat(/"notification_count":\{"following":[0-9]*,"user":([0-9]*)/.exec(rs)[1] || 0),
              },
              sessionid,
              sessionid_sign,
              sessionHash: /"session_hash":"(.*?)"/.exec(rs)[1],
              privateChannel: /"private_channel":"(.*?)"/.exec(rs)[1],
              authToken: /"auth_token":"(.*?)"/.exec(rs)[1],
              joinDate: new Date(/"date_joined":"(.*?)"/.exec(rs)[1] || 0),
            });
          } else err(new Error('Wrong or expired sessionid'));
        });

        res.on('error', err);
      }).end();
    });
  },
gbarton commented 1 year ago

its clugy but I can do a bit of an injection to get the sessionid_sign into the sessionid I pass in and it works. I use dotenv and just pass in whatever I have in my tv_token prop through to token, something like:

let client = new TradingView.Client({ token: process.env.tv_token });

for tv_token it looks like this:

# my token and sign are from the browser, make sure to use yours!
tv_token="mytokenfromthebrowser; sessionid_sign=v1:S6EadHXwm845QDfNn8PVygP7N5yyj4bXDE2MQ3eZg48="

and I'm running again. Don't have to mod the src like above to make it work, but don't forget the quotes if your using props!

Mathieu2301 commented 1 year ago

Fixed it in v3.4.1 ! Thanks to all and thanks to Gaspare Sganga for his pull request #197 😁

I will close this issue if no one has a problem this week.

amayp1 commented 1 year ago

Mine is working fine now thanks guys :)

BlackPhoenixSlo commented 5 months ago

I started getting this error as I am running at the same time 3 or more scripts.

I will chain them 1 by 1.

BlackPhoenixSlo commented 5 months ago
Screenshot 2024-01-28 at 13 31 14

I am getting same problem.

Screenshot 2024-01-28 at 13 34 15

It's like TV does not allow to pull data at the same time.

Screenshot 2024-01-28 at 13 43 38

It looks like TV has the max data you can pull at one time.