11

I'm trying to login to my stackoverflow.com account programatically. I've tried to do a curl like the following but it's asking for content length:

$ curl -X POST --user abc@gmail.com:abc https://stackoverflow.com/users/login -v
*   Trying 151.101.193.69...
* Connected to stackoverflow.com (151.101.193.69) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.stackexchange.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
* Server auth using Basic with user 'abc@gmail.com'
> POST /users/login HTTP/1.1
> Host: stackoverflow.com
> Authorization: Basic YWJjQGdtYWlsLmNvbTphYmM=
> User-Agent: curl/7.43.0
> Accept: */*
> Content-Type:application/json
> 
< HTTP/1.1 411 Length Required
< Content-Type: text/html; charset=us-ascii
< Content-Length: 344
< Accept-Ranges: bytes
< Date: Sat, 10 Sep 2016 14:51:27 GMT
< Via: 1.1 varnish
< Connection: keep-alive
< X-Served-By: cache-sin6924-SIN
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1473519087.232710,VS0,VE240
< X-DNS-Prefetch-Control: off
< Set-Cookie: prov=b46dabec-5ba0-92ef-b6dd-884666bea467; domain=.stackoverflow.com; expires=Fri, 01-Jan-2055 00:00:00 GMT; path=/; HttpOnly
< 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Length Required</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Length Required</h2>
<hr><p>HTTP Error 411. The request must be chunked or have a content length.</p>
</BODY></HTML>
* Connection #0 to host stackoverflow.com left intact

I've also tried without POST. It returns 200 but without login.

$ curl --user abc@gmail.com:abc  https://stackoverflow.com/users/login -v
*   Trying 151.101.193.69...
* Connected to stackoverflow.com (151.101.193.69) port 443 (#0)
* TLS 1.2 connection using TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
* Server certificate: *.stackexchange.com
* Server certificate: DigiCert SHA2 High Assurance Server CA
* Server certificate: DigiCert High Assurance EV Root CA
* Server auth using Basic with user 'abc@gmail.com'
> GET /users/login HTTP/1.1
> Host: stackoverflow.com
> Authorization: Basic YWJjQGdtYWlsLmNvbTphYmM=
> User-Agent: curl/7.43.0
> Accept: */*
> 
< HTTP/1.1 200 OK
< Cache-Control: private
< Content-Type: text/html; charset=utf-8
< X-Frame-Options: SAMEORIGIN
< X-Request-Guid: cb0c93fa-9a05-4661-bc34-c4d97225b13e
< Content-Length: 37753
< Accept-Ranges: bytes
< Date: Sat, 10 Sep 2016 14:53:41 GMT
< Via: 1.1 varnish
< Age: 0
< Connection: keep-alive
< X-Served-By: cache-sin6924-SIN
< X-Cache: MISS
< X-Cache-Hits: 0
< X-Timer: S1473519220.936412,VS0,VE257
< X-DNS-Prefetch-Control: off

How can I accomplish this?

Pankaj Singhal
  • 15,283
  • 9
  • 47
  • 86
  • 1
    You want the API https://api.stackexchange.com/docs/authentication – gelliott181 Sep 11 '16 at 15:39
  • I've already checked the above API. It uses OAuth and it requires redirection from the browser. I don't think we can do that from terminal. Can you suggest me how can I proceed? – Pankaj Singhal Sep 13 '16 at 18:02
  • It doesn't require a redirection, it requires a redirect_uri, or where the user is sent after a successful authentication. In the sections after the two flow overviews, you need the "Desktop Application" heading where they recommend the implicit flow, a redirect_uri of `https://stackexchange.com/oauth/login_success` and mention you'll find your access token in the hash of the redirected url (the part of the url after a # symbol). – gelliott181 Sep 13 '16 at 18:56
  • I'm voting to close this question as off-topic because it belongs on meta – Paul Sweatte Jun 13 '17 at 14:15
  • This might help - https://askubuntu.com/questions/161778/how-do-i-use-wget-curl-to-download-from-a-site-i-am-logged-into – Udayraj Deshmukh May 04 '18 at 07:34
  • Does this answer your question? [How can I log in to Stack Exchange using curl?](https://stackoverflow.com/questions/44154326/how-can-i-log-in-to-stack-exchange-using-curl) – hanshenrik Nov 17 '20 at 20:28

1 Answers1

0

Beside that using the api.stackexchange.com/docs/authentication would be the best solution your problem with the above curl statement is that you send a http post to the server without a body. So you should add something like:

-d "user=value1&passwd=value2"

Where user/passwd should be the field names of the login dialog here - also maybe the login form contains some hidden fields ... so you should analyse that first. Also keep in mind that you let curl write cookies into a file so that you could use them on later curl calls.

PowerStat
  • 3,757
  • 8
  • 32
  • 57