leetcode-cli: Cannot login with premium account

Problem Summary

Recently bought premium and can’t seem to log in anymore.

How to reproduce

Ran ./bin/leetcode user --login -vv Get: [ERROR] invalid password? (403 forbidden)

Here is the full trace:

[TRACE] initializing all plugins
[TRACE] found plugin: cache=default
[TRACE] found plugin: leetcode=default
[TRACE] found plugin: retry=default
[TRACE] inited plugin: leetcode
[TRACE] inited plugin: retry
[TRACE] inited plugin: cache
login: mylogin
pass: 
[DEBUG] running leetcode.login
[DEBUG] running leetcode.signin
⠋ Signing in leetcode.com[TRACE] REQUEST %s { uri: 'https://leetcode.com/accounts/login/',
  callback: [Function] }
[TRACE] REQUEST %s make request https://leetcode.com/accounts/login/
⠸ Signing in leetcode.com[TRACE] REQUEST %s onRequestResponse https://leetcode.com/accounts/login/ 200 { date: 'Sat, 23 Nov 2019 04:51:13 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'set-cookie': [...],
  vary: 'Accept-Encoding, Cookie',
  'x-frame-options': 'SAMEORIGIN',
  'cf-cache-status': 'DYNAMIC',
  'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
  'x-content-type-options': 'nosniff',
  'expect-ct':
   'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  server: 'cloudflare',
  'cf-ray': '...' }
[TRACE] REQUEST %s reading response's body
[TRACE] REQUEST %s finish init function https://leetcode.com/accounts/login/
⠼ Signing in leetcode.com[TRACE] REQUEST %s response end https://leetcode.com/accounts/login/ 200 { date: 'Sat, 23 Nov 2019 04:51:13 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'set-cookie': [...],
  vary: 'Accept-Encoding, Cookie',
  'x-frame-options': 'SAMEORIGIN',
  'cf-cache-status': 'DYNAMIC',
  'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
  'x-content-type-options': 'nosniff',
  'expect-ct':
   'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  server: 'cloudflare',
  'cf-ray': '...' }
[TRACE] REQUEST %s end event https://leetcode.com/accounts/login/
[TRACE] REQUEST %s has body https://leetcode.com/accounts/login/ 32265
[TRACE] REQUEST %s emitting complete https://leetcode.com/accounts/login/
[TRACE] REQUEST %s { url: 'https://leetcode.com/accounts/login/',
  headers:
   { Origin: 'https://leetcode.com',
     Referer: 'https://leetcode.com/accounts/login/',
     Cookie:
      <hidden> },
  form:
   { csrfmiddlewaretoken: '...',
     login: 'redacted_login',
     password: 'redacted_password' },
  callback: [Function],
  method: 'POST' }
[TRACE] REQUEST %s make request https://leetcode.com/accounts/login/
[TRACE] REQUEST %s onRequestResponse https://leetcode.com/accounts/login/ 403 { date: 'Sat, 23 Nov 2019 04:51:14 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'set-cookie': [ ...],
  vary: 'Cookie',
  'x-frame-options': 'SAMEORIGIN',
  'cf-cache-status': 'DYNAMIC',
  'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
  'x-content-type-options': 'nosniff',
  'expect-ct':
   'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  server: 'cloudflare',
  'cf-ray': '...' }
[TRACE] REQUEST %s reading response's body
[TRACE] REQUEST %s finish init function https://leetcode.com/accounts/login/
[TRACE] REQUEST %s response end https://leetcode.com/accounts/login/ 403 { date: 'Sat, 23 Nov 2019 04:51:14 GMT',
  'content-type': 'text/html; charset=utf-8',
  'transfer-encoding': 'chunked',
  connection: 'close',
  'set-cookie': [...],
  vary: 'Cookie',
  'x-frame-options': 'SAMEORIGIN',
  'cf-cache-status': 'DYNAMIC',
  'strict-transport-security': 'max-age=31536000; includeSubDomains; preload',
  'x-content-type-options': 'nosniff',
  'expect-ct':
   'max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"',
  server: 'cloudflare',
  'cf-ray': '...' }
[TRACE] REQUEST %s end event https://leetcode.com/accounts/login/
[TRACE] REQUEST %s emitting complete https://leetcode.com/accounts/login/
[ERROR] invalid password?

Environment

  • leetcode-cli version: 2.6.2-5245886
  • OS version: darwin 18.6.0
  • Node version: v10.16.2
  • Npm version: 6.9.0

About this issue

Most upvoted comments

It seems I’m able to get the cli working by manually logging into the site in a browser, viewing the cookies, and creating the ~/.lc/leetcode/user.json by hand. My file looks like …

{
  "login": "[username]",
  "loginCSRF": "",
  "sessionCSRF": "[copied from csrftoken]",
  "sessionId": "[copied from LEETCODE_SESSION]"
}

Currently, I’ve only tested showing my account status and listing problems.

most probably, leetcode recently enhance the security set of its API, that’s why our login request failed. I am trying to fix this issue, but no luck yet…

For those who want to find data below, just go to chrome and paste this chrome://settings/cookies/detail?site=leetcode.com

{
  "login": "[username]",
  "loginCSRF": "",
  "sessionCSRF": "[copied from csrftoken]",
  "sessionId": "[copied from LEETCODE_SESSION]"
}

Edit:

It seems like the link above no longer works: Go to leetcode.com and open inspect tab Paste this in the console and press enter: Object.fromEntries(document.cookie.split('; ').map(v=>v.split(/=(.*)/s).map(decodeURIComponent)))

Then you should see it return your cookies. You can then copy sessionCSRF from csrftoken and sessionId from [xxxxxxxx]_gr_session_id

You can try this workshop based on leetcode-cli for workaround: https://github.com/luxp/leetcode-workshop-javascript

Instead of submit with cli, it will automatically copy the submit content to the clipboard and open the problem page. You just need to paste it.

And by the way, you can use jest to test your code locally, which is really helpful and fast for debug and verification.

For those who want to find data below, just go to chrome and paste this chrome://settings/cookies/detail?site=leetcode.com

{
  "login": "[username]",
  "loginCSRF": "",
  "sessionCSRF": "[copied from csrftoken]",
  "sessionId": "[copied from LEETCODE_SESSION]"
}

love u bro

I sovled this problem by using cookie.chrome plugin. Seems need to disable some other plugins to make it works.

leetcode plugin -d leetcode.cn
leetcode plugin  -d lintcode
leetcode user -l -v

It seems I’m able to get the cli working by manually logging into the site in a browser, viewing the cookies, and creating the ~/.lc/leetcode/user.json by hand. My file looks like …

{
  "login": "[username]",
  "loginCSRF": "",
  "sessionCSRF": "[copied from csrftoken]",
  "sessionId": "[copied from LEETCODE_SESSION]"
}

Currently, I’ve only tested showing my account status and listing problems.

I am trying your tips. But I don’t know how can I find my LEETCODE_SESSION. Could you tell me where I can find it?

After logging into LeetCode I’m using the storage inspector to grab the cookie values, https://developer.mozilla.org/en-US/docs/Tools/Storage_Inspector.