Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use JSON login? #249

Open
TheI1 opened this issue Oct 12, 2024 · 3 comments
Open

Why not use JSON login? #249

TheI1 opened this issue Oct 12, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@TheI1
Copy link

TheI1 commented Oct 12, 2024

I can see that the login function in session.py uses the login API to get the session ID by requesting the entire page.

I was wondering why not just get the JSON response from the login API, which includes the x-token already, the same way the browser does when you login using the box on the top-right?

Also I was curious why you did not not use the "json" and "cookies" parameters for the requests functions instead of setting the data to a json.dumps result and adding cookies to the header?

This is a (working) example of how I implemented this request for one of my personal projects:

request = requests.post(
    "https://scratch.mit.edu/accounts/login/",
    json={
        "username": username,
        "password": password
    },
    headers={
        "x-requested-with": "XMLHttpRequest",
        "referer": "https://scratch.mit.edu/",
        "x-csrftoken": token
    },
    cookies={
        "scratchcsrftoken": token
    }
)

and request.json()[0]["token"] to get the x-token. The session ID can still be obtained the same way through "Set-Cookie".

@TimMcCool TimMcCool added the enhancement New feature or request label Oct 12, 2024
@TimMcCool
Copy link
Owner

I didn't use scratch.mit.edu/accounts/login because I couldn't get it to work for some reason. I always get [{'username': '', 'num_tries': 0, 'success': 0, 'msg': '', 'messages': [], 'id': None}] from the API.

@Grisshink
Copy link
Contributor

@TimMcCool Try adding the trailing slash at the end of url like this -> https://scratch.mit.edu/accounts/login/. I learned this myself the hard way and this small thing was the entire cause of this problem

@TimMcCool
Copy link
Owner

Thanks, I'll try that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants