S
S
Sergey Galitsky2020-05-11 23:33:09
Python
Sergey Galitsky, 2020-05-11 23:33:09

How to log in to habr using requests?

There was a need to write a parser to display all publications by subscription. However, I ran into a problem at the very beginning: authorization. Next, I will describe the general approach to solving this problem with the main headers and data sent along with the request:
1) GET request to the authorization page
2) POST request to https://account.habr.com/ajax/validate/email/
headers = {
"Accept":"*/*",
"Accept-Encoding" : "gzip, deflate, br",
"Accept-Language":"en-RU,ru;q=0.8,en-US;q= 0.5,en;q=0.3",
"Connection":"keep-alive",
"Content-Length":"43",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF- eight",
"Origin":" https://account.habr.com ",
"Referer":" https://account.habr.com/login/?state= <sequence> &consumer=habr&hl=ru_RU",
"User-Agent ":"Mozilla/5.0 Gecko/20100101 Firefox/75.0",
"X-Requested-With":"XMLHttpRequest"
}

data = {
"value": <mail> ,
"name":"email"
}

Note: as an answer arrives: {"messages":"ok"}
3) POST request to https://account.habr.com/ajax/login/
headers = {
"Host": "account.habr.com",
"User-Agent": "Mozilla/5.0 Gecko/20100101 Firefox/75.0",
"Accept": "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01",
"Accept-Language": "ru-RU,ru;q=0.8,en -US;q=0.5,en;q=0.3",
"Accept-Encoding": "gzip, deflate, br",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8 ",
"X-Requested-With": "XMLHttpRequest",
"Content-Length": "170",
"Origin": " https://account.habr.com ",
"DNT": "1",
"Connection ": "keep-alive",
"Referer": " https://account.habr.com/login/?state= <sequence> &consumer=habr&hl=ru_RU",
"Cookie":
}

data = {
"state":<sequence> ,
"consumer":"habr",
"email": <mail> ,
"password": <password> ,
"captcha":"",
"g-recaptcha-response":"",
"captcha_type": "recaptcha"
}
Note: the following arrives as a response: {"messages":"ok"}
4) GET request to https://habr.com/ru/feed/ with cookie (obtained from step #3)

However, still general publications are displayed, not by subscription. I assume that the whole problem is with tokens, because no idea how to use them. Or maybe, problem in the general approach? I would be grateful for any information. Thank you for your time!

UPDATE: The answer in point 3, if working correctly, should return json starting with window.location.href

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-05-12
@dimonchik2013

Postman

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question