Skip to content

[ERROR] http error [code=400] #489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
huang-kun opened this issue Dec 7, 2019 · 27 comments · Fixed by leetcode-tools/leetcode-cli#32
Closed

[ERROR] http error [code=400] #489

huang-kun opened this issue Dec 7, 2019 · 27 comments · Fixed by leetcode-tools/leetcode-cli#32
Labels
bug Something isn't working
Milestone

Comments

@huang-kun
Copy link

I install the leetcode plugin from vscode and login successfully with leetcode-cn account, but it fails downloading contents.

- Downloading problems
- Downloading questions titles
[ERROR] http error [code=400]

I like this plugin. It really improves leetcode experience. But it doesn't work now. Can you help me?

@ws2020
Copy link

ws2020 commented Dec 7, 2019

+1

1 similar comment
@lemongeek
Copy link

+1

@MaybeSilent
Copy link

MaybeSilent commented Dec 7, 2019

I also met this problem

@RenZMin
Copy link

RenZMin commented Dec 7, 2019

i met this problem too

@arron7856d
Copy link

+1

4 similar comments
@tumumu-awesome
Copy link

+1

@pwf2006
Copy link

pwf2006 commented Dec 7, 2019

+1

@liushaoxiong10
Copy link

+1

@zhanghengjie1993
Copy link

+1

@yihong0618
Copy link
Contributor

Hi @ALL @jdneo
I try to locate this bug.
And I found that leetcode-cn just change their payload in here.
image
I try to change it to new payload, found it worked, and I need more time for debug it.
image
But today I'm a little busy and I will find someway maybe tomorrow night~

@yihong0618
Copy link
Contributor

yihong0618 commented Dec 7, 2019

If you want to quick fix this you can find the file in plugins/leetcode.cn.js , you can change the payload like mine then change the question.title below(I forget the very name) because I have no computer right now~

@YinDongFang
Copy link

Hi @ALL @jdneo
I try to locate this bug.
And I found that leetcode-cn just change their payload in here.
image
I try to change it to new payload, found it worked, and I need more time for debug it.
image
But today I'm a little busy and I will find someway maybe tomorrow night~

change code at line 117 to following:
body.data.translations.forEach(function(x) {
titles[x.questionId] = x.title;
});

@yihong0618
Copy link
Contributor

yihong0618 commented Dec 7, 2019

Hi @ALL @jdneo
I try to locate this bug.
And I found that leetcode-cn just change their payload in here.
image
I try to change it to new payload, found it worked, and I need more time for debug it.
image
But today I'm a little busy and I will find someway maybe tomorrow night~

change code at line 117 to following:
body.data.translations.forEach(function(x) {
titles[x.questionId] = x.title;
});

Yes sorry my laptop is power off ...

@jjeejj
Copy link

jjeejj commented Dec 7, 2019

I also met this problem on my Mac pro
but window system is right

leetcode extension version is latest
vscode 1.40.2
Node.js: 12.4.0
mac 10.15.1 (19B88)

@bubble93
Copy link

bubble93 commented Dec 7, 2019

Where is plugin / leetcode-cn.js?
I searched the whole computer. We can't find it.

@yihong0618
Copy link
Contributor

Where is plugin / leetcode-cn.js?
I searched the whole computer. We can't find it.

sorry,I don't remember the filename. maybe leetcode.cn.js(filename)

@zhanghengjie1993
Copy link

zhanghengjie1993 commented Dec 7, 2019

Where is plugin / leetcode-cn.js?
I searched the whole computer. We can't find it.

the path in my environment is C:\Users\hengjie\.vscode\extensions\shengchen.vscode-leetcode-0.15.7\node_modules\vsc-leetcode-cli\lib\plugins\leetcode.cn.js

@ljw-struggle
Copy link

Mark!

@bubble93
Copy link

bubble93 commented Dec 7, 2019

Where is plugin / leetcode-cn.js?
I searched the whole computer. We can't find it.

the path in my environment is C:\Users\hengjie.vscode\extensions\shengchen.vscode-leetcode-0.15.7\node_modules\vsc-leetcode-cli\lib\plugins\leetcode.cn.js

Thank you very much. I found it

@shihongji
Copy link

Sorry, is this right?
Still not work
image

@yihong0618
Copy link
Contributor

yihong0618 commented Dec 8, 2019

Sorry, is this right?
Still not work
image

you have to change x.question.questionId to x.questionId and the payload (should exactly like mine

@RenZMin
Copy link

RenZMin commented Dec 8, 2019

Sorry, is this right?
Still not work
image
i cha

Sorry, is this right?
Still not work
image

you have to change x.question.questionId to x.questionId and the payload (should exactly like mine

1575772617(1)
sorry,,i have changed x.question.questionId to x.questionId and the payload 。the picture is my leetcode.cn.js.But it didn't work. Is there anything with my changes? Thank you.

@yihong0618
Copy link
Contributor

yihong0618 commented Dec 8, 2019

@RenZMin
You have one more field “question” and the format is wrong ------more spaces
And I have a pr, you can copy my code

@YinDongFang
Copy link

YinDongFang commented Dec 8, 2019

default leetcode.cn.js path:
C:\Users\[your user name]\.vscode\extensions\shengchen.vscode-leetcode-0.15.7\node_modules\vsc-leetcode-cli\lib\plugins\leetcode.cn.js

'use strict'
var request = require('request');

var config = require('../config');
var h = require('../helper');
var log = require('../log');
var Plugin = require('../plugin');
var session = require('../session');

//
// [Usage]
//
// https://github.com/skygragon/leetcode-cli-plugins/blob/master/docs/leetcode.cn.md
//
var plugin = new Plugin(15, 'leetcode.cn', '2018.11.25',
    'Plugin to talk with leetcode-cn APIs.');

plugin.init = function() {
  config.app = 'leetcode.cn';
  config.sys.urls.base            = 'https://leetcode-cn.com';
  config.sys.urls.login           = 'https://leetcode-cn.com/accounts/login/';
  config.sys.urls.problems        = 'https://leetcode-cn.com/api/problems/$category/';
  config.sys.urls.problem         = 'https://leetcode-cn.com/problems/$slug/description/';
  config.sys.urls.graphql         = 'https://leetcode-cn.com/graphql';
  config.sys.urls.problem_detail  = 'https://leetcode-cn.com/graphql';
  config.sys.urls.test            = 'https://leetcode-cn.com/problems/$slug/interpret_solution/';
  config.sys.urls.session         = 'https://leetcode-cn.com/session/';
  config.sys.urls.submit          = 'https://leetcode-cn.com/problems/$slug/submit/';
  config.sys.urls.submissions     = 'https://leetcode-cn.com/api/submissions/$slug';
  config.sys.urls.submission      = 'https://leetcode-cn.com/submissions/detail/$id/';
  config.sys.urls.verify          = 'https://leetcode-cn.com/submissions/detail/$id/check/';
  config.sys.urls.favorites       = 'https://leetcode-cn.com/list/api/questions';
  config.sys.urls.favorite_delete = 'https://leetcode-cn.com/list/api/questions/$hash/$id';
};

// FIXME: refactor those
// update options with user credentials
function signOpts(opts, user) {
  opts.headers.Cookie = 'LEETCODE_SESSION=' + user.sessionId +
                        ';csrftoken=' + user.sessionCSRF + ';';
  opts.headers['X-CSRFToken'] = user.sessionCSRF;
  opts.headers['X-Requested-With'] = 'XMLHttpRequest';
}

function makeOpts(url) {
  const opts = {};
  opts.url = url;
  opts.headers = {};

  if (session.isLogin())
    signOpts(opts, session.getUser());
  return opts;
}

function checkError(e, resp, expectedStatus) {
  if (!e && resp && resp.statusCode !== expectedStatus) {
    const code = resp.statusCode;
    log.debug('http error: ' + code);

    if (code === 403 || code === 401) {
      e = session.errors.EXPIRED;
    } else {
      e = {msg: 'http error', statusCode: code};
    }
  }
  return e;
}

plugin.getProblems = function(cb) {
  plugin.next.getProblems(function(e, problems) {
    if (e) return cb(e);

    plugin.getProblemsTitle(function(e, titles) {
      if (e) return cb(e);

      problems.forEach(function(problem) {
        const title = titles[problem.id];
        if (title)
          problem.name = title;
      });

      return cb(null, problems);
    });
  });
};

plugin.getProblemsTitle = function(cb) {
  log.debug('running leetcode.cn.getProblemNames');

  const opts = makeOpts(config.sys.urls.graphql);
  opts.headers.Origin = config.sys.urls.base;
  opts.headers.Referer = 'https://leetcode-cn.com/api/problems/algorithms/';

  opts.json = true;
  opts.body = {
    query: [
      'query getQuestionTranslation($lang: String) {',
      '  translations: allAppliedQuestionTranslations(lang: $lang) {',
      '    title',
      '    questionId',
      '  }',
      '}',
      ''
    ].join('\n'),
    variables:     {},
    operationName: 'getQuestionTranslation'
  };

  const spin = h.spin('Downloading questions titles');
  request.post(opts, function(e, resp, body) {
    spin.stop();
    e = checkError(e, resp, 200);
    if (e) return cb(e);

    const titles = [];
    body.data.translations.forEach(function(x) {
      titles[x.questionId] = x.title;
    });

    return cb(null, titles);
  });
};

module.exports = plugin;

@jdneo jdneo added the bug Something isn't working label Dec 8, 2019
@jdneo jdneo added this to the 0.15.8 milestone Dec 8, 2019
@hongguang-alt
Copy link

I just npm i leetcode-cli -g and it work

@Daisybiubiubiu
Copy link

same Error 400 issue.. the updated version already fixed the file leetcode.cn.js, but I still got this error message.

@jackielics
Copy link

I just npm i leetcode-cli -g and it work

I'll give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.