Skip to content
This repository has been archived by the owner on Aug 1, 2021. It is now read-only.

Fix 400 Bad Request when login with username and password #382

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions udemy/_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

'''

import re
from pprint import pprint
from ._session import Session
from ._compat import (
Expand Down Expand Up @@ -71,11 +72,14 @@ def _form_hidden_input(self, form_id):

def authenticate(self, access_token='', client_id=''):
if not access_token and not client_id:
data = {'email' : self.username, 'password' : self.password}
res = self._session._get(LOGIN_URL)
csrf = re.search(r'(?i)(?:csrfmiddlewaretoken\' value=\'(?P<csrf_token>[a-zA-Z0-9]+))', res.text).group(
'csrf_token')
data = {'email': self.username, 'password': self.password, 'csrfmiddlewaretoken': csrf}
auth_response = self._session._post(LOGIN_URL, data=data)
auth_cookies, auth_response = auth_response.cookies, auth_response.json()
access_token = auth_response.get('access_token', '')
auth_cookies = auth_response.history[0].cookies

access_token = auth_cookies.get('access_token', '')
client_id = auth_cookies.get('client_id', '')

if access_token:
Expand Down
16 changes: 11 additions & 5 deletions udemy/_compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@


NO_DEFAULT = object()
LOGIN_URL = 'https://www.udemy.com/api-2.0/auth/udemy-auth/login/?fields[user]=access_token'
# LOGIN_URL = 'https://www.udemy.com/api-2.0/auth/udemy-auth/login/?fields[user]=access_token'
LOGIN_URL = 'https://www.udemy.com/join/login-popup/?displayType=ajax&display_type=popup&showSkipButton=1&returnUrlAfterLogin=https'
LOGOUT_URL = 'https://www.udemy.com/user/logout'

WISHLIST_URL = "https://{portal_name}.udemy.com/api-2.0/users/me/wishlisted-courses?fields[course]=id,url,published_title&ordering=-access_time&page=1&page_size=1000"
Expand All @@ -75,11 +76,16 @@
COURSE_SEARCH = "https://{portal_name}.udemy.com/api-2.0/users/me/subscribed-courses?fields[course]=id,url,published_title&page=1&page_size=1000&ordering=-access_time&search={course_name}"
COURSE_URL = 'https://{portal_name}.udemy.com/api-2.0/courses/{course_id}/cached-subscriber-curriculum-items?fields[asset]=results,external_url,time_estimation,download_urls,slide_urls,filename,asset_type,captions,stream_urls,body&fields[chapter]=object_index,title,sort_order&fields[lecture]=id,title,object_index,asset,supplementary_assets,view_html&page_size=10000'
HEADERS = {
'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.21 (KHTML, like Gecko) Mwendo/1.1.5 Safari/537.21',
'X-Requested-With' : 'XMLHttpRequest',
'Host' : 'www.udemy.com',
'Host': 'www.udemy.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0',
'Referer': 'https://www.udemy.com/join/login-popup/',
'Accept': '*/*',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
# 'X-Requested-With' : 'XMLHttpRequest',
# This header is taken from https://github.com/FaisalUmair/udemy-downloader-gui thanks to @FaisalUmair for quick help.
"Authorization": "Basic YWQxMmVjYTljYmUxN2FmYWM2MjU5ZmU1ZDk4NDcxYTY6YTdjNjMwNjQ2MzA4ODI0YjIzMDFmZGI2MGVjZmQ4YTA5NDdlODJkNQ=="
# "Authorization": "Basic YWQxMmVjYTljYmUxN2FmYWM2MjU5ZmU1ZDk4NDcxYTY6YTdjNjMwNjQ2MzA4ODI0YjIzMDFmZGI2MGVjZmQ4YTA5NDdlODJkNQ=="
}


Expand Down