0

So I'm learning my way around web crawlers and automatization. I'm trying to automate the login for mega.nz without using their official API (as far as I'm concerned it's even outdated for Python).

And this one is the code (currently not working since no data is really being sent). I don't want to use a browser.

loginURL = 'https://mega.nz/login'
requestURL = 'https://mega.nz/fm/dashboard'

payload = {
    'login-name2': 'test@email.com',
    'login-password2': 'password',
    'login-check2': ''
}

with requests.session() as s:
     s.post(loginURL, headers=headers, data=payload)
     r = s.get(requestURL)
     print(r.text)

Headers I am using:

{'Content-Type': 'text/html', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Headers': 'MEGA-Chrome-Antileak', 'Access-Control-Max-Age': '86400', 'Content-Encoding': 'gzip', 'Content-Length': '934', 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains; preload', 'X-Frame-Options': 'DENY', 'Set-Cookie': 'geoip=IT', 'Content-Security-Policy': "default-src 'self' data: blob: *.mega.co.nz *.mega.nz http://*.mega.co.nz http://*.mega.nz wss://*.karere.mega.nz *.karere.mega.nz:1380 http://127.0.0.1:6341 localhost.megasyncloopback.mega.nz:6342; script-src 'self' *.mega.co.nz *.mega.nz data: blob:; style-src 'self' 'unsafe-inline' *.mega.co.nz *.mega.nz data: blob:; frame-src 'self' mega: *.megaad.nz; img-src 'self' *.mega.co.nz *.mega.nz data: blob:", 'Connection': 'Keep-Alive'}

How can I automate signing into this?

Jack Moody
  • 1,590
  • 3
  • 21
  • 38
rhegrate
  • 1
  • 1
  • 3
  • Possible duplicate of [Login to website using python requests](https://stackoverflow.com/questions/43285622/login-to-website-using-python-requests) – Jack Moody Jan 16 '19 at 17:28

3 Answers3

2

Try this https://github.com/richardARPANET/mega.py/

It worked for me.

There is an example step by step on README.rst

Alambek
  • 85
  • 8
0

You may want to read requests documentation about Authentication, it gives a few methods to authenticate through HTTP.

You can find it here.

Rémi Héneault
  • 383
  • 3
  • 12
0

I did not find any information about an HTTP API for Mega. Are you trying to get access to the site via the web interface that you would use in your browser? It could be very difficult or impossible to get your software working this way.

Usually you would only use plain HTTP requests when the service you try to access provides a working REST API. (see for example the Spotify API)

Maybe take a look at this example for accessing Mega from Python. This uses the official Mega C++ API which you can access from your Python script.

Florian Lüdiger
  • 168
  • 3
  • 12
  • Yes, I'm trying to access the service using plain HTTP requests so I can use Python to loop through my personal files. I don't think an API is always required to access a website, am I right? I just want to get the HTML of a specific page and visualize some content of it in Python. And in order to do that, I need to access the website sice the page is hidden. If I don't want to use my browser, what can I do? – rhegrate Jan 16 '19 at 17:59
  • @rhegrate The problem in this case is that viewing the list of files in the browser requires you to be logged in, aka that you have a special cookie saved in your browser configuration. Mimicking this behavior from within python without using a browser is pretty difficult as far as I know. Using the example from the link in my answer would make this far more easy because it shows you how to access information like directory structure and files from within python. – Florian Lüdiger Jan 16 '19 at 18:07