J
J
JHik2017-01-04 21:00:26
YouTube
JHik, 2017-01-04 21:00:26

Authorization on YouTube using the Requests module?

And so, my problem with authorization on YouTube:
To begin with, I will clarify:
I do not use / will not use the YouTube api. (because the api does not fully satisfy my requirements)
The essence of the problem in the authorization window:

import requests

headers = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"

class YouTube:

    def __init__(self, username, password):
        self.username = username
        self.password = password
        LOGIN_FORM = {
           ... # ВСЕ данные, все данные были взяты с помощью Google Chrome Dev Tools
        }
        with requests.Session() as s:
            s.headers = self.headers
            s.post(url=LOGIN_URL, data=LOGIN_FORM)
            print(s.text) # Окно авторизации
            print(s.url) # тот же URL, без редиректа, ничего не произошло.
            print(s.get('https://myaccount.google.com/?pli=1').text) # возвращает в окно авторизации
    .... #####

The problem is that the POST request doesn't work, it doesn't do anything.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alex maslakoff, 2017-01-04
@JHik

возможно, без Selenium или headless browser тут не обойтись. Например, в гугле много чего нельзя сделать просто отправив запрос, если не через api, там еще нужно чтобы js выполнился в браузере сначала.

P
PANDEMIC, 2017-02-11
@InvictusManeo

Here is an example of a request for mail ru
The whole point is that the headers are different for everyone and the authorization page is the same. Look in the browser for GET and POST requests. Where and what do they send? Well, substitute them in requests. Of course, today, almost everyone has switched to sessions, cookies, tokens.

with requests.Session() as session:
  # start of auth
  s = Session()
  AUTH_URL = 'https://auth.mail.ru/cgi-bin/auth'
  headers = {
    'Login': 'втойящик@mail.ru',
    'Password': 'твой пароль',
    'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11.6; rv:51.0.1) Gecko/20100101 Firefox/51.0.1',
    'Content-Type': 'application/x-www-form-urlencoded',
    'Connection': 'keep-alive',
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Referer': 'http://mail.ru/',
    'remember': 1,
  }
  # Отправляем данные в POST, в session записываются наши куки
  s.post(AUTH_URL, headers)
  # end auth

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question