Creates a session / log in to the Scratch website with the specified username and password. This method ... 1. creates a session id by posting a login request to Scratch's login API. (If this fails, scratchattach.exceptions.LoginFailure is raised) 2. fetches the xtoken and other in
(username, password, *, timeout=10)
| 1398 | |
| 1399 | |
| 1400 | def login(username, password, *, timeout=10) -> Session: |
| 1401 | """ |
| 1402 | Creates a session / log in to the Scratch website with the specified username and password. |
| 1403 | |
| 1404 | This method ... |
| 1405 | 1. creates a session id by posting a login request to Scratch's login API. (If this fails, scratchattach.exceptions.LoginFailure is raised) |
| 1406 | 2. fetches the xtoken and other information by posting a request to scratch.mit.edu/session. (If this fails, a warning is displayed) |
| 1407 | |
| 1408 | Args: |
| 1409 | username (str) |
| 1410 | password (str) |
| 1411 | |
| 1412 | Keyword arguments: |
| 1413 | timeout (int): Timeout for the request to Scratch's login API (in seconds). Defaults to 10. |
| 1414 | |
| 1415 | Returns: |
| 1416 | scratchattach.session.Session: An object that represents the created login / session |
| 1417 | """ |
| 1418 | issue_login_warning() |
| 1419 | |
| 1420 | # Post request to login API: |
| 1421 | _headers = headers.copy() |
| 1422 | _headers["Cookie"] = "scratchcsrftoken=a;scratchlanguage=en;" |
| 1423 | with requests.no_error_handling(): |
| 1424 | request = requests.post( |
| 1425 | "https://scratch.mit.edu/login/", |
| 1426 | json={"username": username, "password": password}, |
| 1427 | headers=_headers, |
| 1428 | timeout=timeout, |
| 1429 | ) |
| 1430 | |
| 1431 | try: |
| 1432 | result = re.search('"(.*)"', request.headers["Set-Cookie"]) |
| 1433 | assert result is not None |
| 1434 | session_id = str(result.group()) |
| 1435 | except Exception: |
| 1436 | raise exceptions.LoginFailure( |
| 1437 | "Either the provided authentication data is wrong or your network is banned from Scratch.\n\nIf you're using an online IDE (like replit.com) Scratch possibly banned its IP address. In this case, try logging in with your session id: https://github.com/TimMcCool/scratchattach/wiki#logging-in" |
| 1438 | ) |
| 1439 | |
| 1440 | # Create session object: |
| 1441 | with suppress_login_warning(): |
| 1442 | return login_by_id(session_id, username=username, password=password) |
| 1443 | |
| 1444 | |
| 1445 | def login_by_session_string(session_string: str) -> Session: |
no test coverage detected