skydiver / ewelink-api

eWeLink API for JavaScript
https://www.npmjs.com/package/ewelink-api
MIT License
264 stars 108 forks source link

Error 401: Wrong account or password #212

Closed besynnerlig closed 1 year ago

besynnerlig commented 1 year ago

I'm using the ewelink-api library to control my eWeLink devices (actually my neighbor's devices that have been shared to my account), but I'm getting an error 401 ("Wrong account or password") when trying to authenticate. I have verified that my account and password are correct, and I can log in to the eWeLink app with the same credentials. I also seem to be able to toggle the devices ON/OFF, at least there are no errors shown in the app.

Here are the steps to reproduce the issue:

Initialize the ewelink-api library with my account and password. Create the connection, seems to work. connection.setDevicePowerState fails with error: {"error":401,"msg":"Wrong account or password"}

The library returns an error 401, indicating that the account or password is incorrect. I have also checked the this GitHub repository and did not find any reported issues that match my problem.

This error started to occur today without that I have changed anything regarding to my credentials.

Please let me know if there are any suggestions for troubleshooting or if there is any additional information that I can provide to help diagnose the issue.

Below is the source code that I use:

require('dotenv').config();
const ewelink = require('ewelink-api');
const Push = require('pushover-notifications');
const winston = require('winston');

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)
  ),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'logs/ewelink-api.log' })
  ]
});

const pushover = new Push({
  user: process.env.PUSHOVER_USER_KEY,
  token: process.env.PUSHOVER_API_TOKEN
});

const ewelinkCredentials = {
  email: process.env.EWELINK_EMAIL,
  password: process.env.EWELINK_PASSWORD,
  region: 'eu',
};

const sleepDuration = 10000;
const maxTries = 3;

async function setDevicePowerState(deviceId, newState) {
  try {
    var connection = new ewelink(ewelinkCredentials);
    await sleep(sleepDuration);

    let tries = 0;
    let state;
    do {
      state = await connection.setDevicePowerState(deviceId, newState);
      var stateJson = JSON.stringify(state);

      logger.info(`Set device ${deviceId} to ${newState}, returned: ${stateJson}`);

      if (state.error == null && state.status == 'ok') {
        // Wait for the server to update the device's state
        await sleep(sleepDuration);

        var status = await connection.getDevicePowerState(deviceId);
        if (status.error == null && status.state === newState) {
          logger.info(`Device ${deviceId} power state was confirmed`);
          break;
        }
      }

      tries++;
      if (tries <= maxTries) {
        logger.warn(`Failed to set device ${deviceId} to ${newState}`);
        if (state.error === 401 || state.error === 406 || state.error === 503) {
          connection = await reconnect(connection, ewelinkCredentials);
        }
      } else {
        const msg = {
          message: `${deviceId}, ${newState} -> ${stateJson}`,
          title: "ewelink-api",
          priority: 1
        };
        pushover.send(msg, function (err, result) {
          if (err) {
            logger.error(`Error sending Pushover notification: ${err}`);
            return;
          }
          logger.info(`Pushover notification sent: ${result}`);
        });
      }
    } while (tries <= maxTries);
  } catch (error) {
    logger.error(`Error setting device ${deviceId} power state to ${newState}: ${error}`);
  }
}

function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

async function reconnect(connection, ewelinkCredentials) {
    logger.warn(`Reconnecting to ewelink server...`);
    // connection.close() Not available it seems...
    await sleep(sleepDuration);
    connection = new ewelink(ewelinkCredentials);
    return connection;
  }

(async () => {
  const args = require('minimist')(process.argv.slice(2));
  const deviceId = args.deviceid.toString();
  const newState = args.newstate;

  await setDevicePowerState(deviceId, newState);
})();
LiorLindvor commented 1 year ago

Same

BankTP commented 1 year ago

Me too. anyone known what happen?

maxpavlovdp commented 1 year ago

we have similar issue with 408 error

appId: YzfeftUVcZ6twZw1OoVKPRFYTrGEg01Q

API responded with error=408, msg=appid has expired

dhm-msd commented 1 year ago

Same issue here.

Note: Not all calls raise that error. I am able to getCredentials and getDevices without an error. All other calls fail with { error: 401, msg: 'Wrong account or password' }

BankTP commented 1 year ago

I can list devices and everything else but toggle device and get/set power stat return 401 Wrong account or password

limsongyu commented 1 year ago

same

IvanTroshka commented 1 year ago

I have the same problem. It looks like an account check has been added to the request to change the device status.

esalu commented 1 year ago

same

davidhl commented 1 year ago

same

apadilla83 commented 1 year ago

same, problems again? too bad ewelink...

Pandg-2019 commented 1 year ago

Same for me

const connection = new ewelink({ email: 'xxx@yyy', password: 'zzz', region:'eu', });

{ error: 401, msg: 'Wrong account or password' }

I can list devices or get region but I cant run toggle

tmbooh commented 1 year ago

Same [408] appid has expired

mhborgatto commented 1 year ago

Same for me

{ error: 401, msg: 'Wrong account or password' }

I can list devices or get region but I cant run toggle

glaliberte commented 1 year ago

Same [408] appid has expired

LiorLindvor commented 1 year ago

After a long research, the AppID was Expired, I found out some APP_ID's and APP_SECRET's that was left around in GitHub, use on your own risk..

APP_ID: oeVkj2lYFGnJu5XUtWisfW4utiN4u9Mq
APP_SECRET: 6Nz4n0xA8s8qdxQf2GqurZj2Fs55FUvM
APP_ID: R8Oq3y0eSZSYdKccHlrQzT1ACCOUT9Gv
APP_SECRET: 1ve5Qk9GXfUhKAn1svnKwpAlxXkMarru
APP_ID: Uw83EKZFxdif7XFXEsrpduz5YyjP7nTl 
APP_SECRET: mXLOjea0woSMvK9gw7Fjsy7YlFO4iSu6

to use it, just add the APP_ID and APP_SECRET under the initialization of the class,

new ewelink({
    email: process.env.EWE_EMAIL || `test`,
    password: process.env.EWE_PASS || `test`,
    region: process.env.EWE_REGION || `test`,
    APP_ID: '...',
    APP_SECRET: '...',
  });

I also registered as a developer to generate my own appid, I think this is the best way to fix the issue.

esalu commented 1 year ago

Uw83EKZFxdif7XFXEsrpduz5YyjP7nTl works for me... and I also registered as a developer at https://dev.ewelink.cc to create my own app id

Mr-KoKa commented 1 year ago

Same issue, using app secret above works and I guess it will work until something else, seemingly random, will break out of nowhere, gotta love ewelink-api

maxpavlovdp commented 1 year ago

ewelink has been extended appid YzfeftUVcZ6twZw1OoVKPRFYTrGEg01Q

I think issue can be closed.

besynnerlig commented 1 year ago

Uw83EKZFxdif7XFXEsrpduz5YyjP7nTl works for me... and I also registered as a developer at https://dev.ewelink.cc to create my own app id

This is what I did and hopefully I'll get a message before my personal app id eventually expires. I close this issue now as the problem seems to be fixed.

antonioGabrielGomes commented 10 months ago

also registered as a developer to generate my own appid, after registering your ewelink account and creating app, did your app_id and secret_id work normally?

mduchain commented 10 months ago

@LiorLindvor I've created my own APP_ID & APP_SECRET on dev.ewelink.cc, - standard role. However this doens't fully work. I can get list of devices and events just fine but all other nodes just give me "wrong account or password" even though the getCredentials promise is successfull in all cases, and all nodes show the green dot. With the APP_ID/SECRET form your message, all functionality is restored.

So perhaps the role is an issue? Other possibility: the ewlink API has changed, ewelink-api still uses the v1.