I'm trying to login to Dropbox (dropbox.com/login) via mechanize on Python. I think I'm missing something as the response returns as HTTP Forbidden 403 Error.
This is the code I used:
import mechanize
url = "https://www.dropbox.com/login"
email = "<USERNAME>"
password = "<PASSWORD>"
br = mechanize.Browser()
br.set_handle_equiv(False)
br.set_handle_gzip(False)
br.set_handle_redirect(False)
br.set_handle_referer(False)
br.set_handle_robots(False)
br.addheaders = [('Host', 'www.dropbox.com')]
br.addheaders = [('Connection', 'keep-alive')]
#br.addheaders = [('Content-Length', '7385')]
br.addheaders = [('Upgrade-Insecure-Requests', 1)]
br.addheaders = [('Origin', 'https://www.dropbox.com')]
br.addheaders = [('X-Requested-With', 'XMLHttpRequest')]
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36')]
br.addheaders = [('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8')]
br.addheaders = [('Content-Type', 'application/x-www-form-urlencoded;
charset=UTF-8')]
br.addheaders = [('Referer', 'https://www.dropbox.com/login?
cont=https%3A%2F%2Fwww.dropbox.com')]
br.addheaders = [('Accept-Encoding', 'gzip, deflate, br')]
br.addheaders = [('Accept-Language', 'en-US,en;q=0.9')]
br.addheaders = [('Cookie', '<COOKIE DATA>')]
response = br.open(url)
try:
formcount=0
for frm in br.forms():
if str(frm.attrs["class"])=="clearfix credentials-form login-form":
break
formcount=formcount+1
br.select_form(nr=formcount)
br.form.set_all_readonly(False)
except:
print("Unable to find login form.");
exit(1);
br.form['login_email'] = email
br.form['login_password'] = password
response = br.submit(nr = formcount)
print response.text
And this is the response:
Traceback (most recent call last):
File "login.py", line 51, in <module>
response = br.submit(nr = formcount)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 685, in
submit
return self.open(self.click(*args, **kwds))
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 254, in
open
return self._mech_open(url_or_request, data, timeout=timeout)
File "C:\Python27\lib\site-packages\mechanize\_mechanize.py", line 310, in
_mech_open
raise response
mechanize._response.httperror_seek_wrapper: HTTP Error 403: Forbidden
I know I'm missing something but can't figure out what. *I'll note that I copied the headers from an intercepted request I made in the browser. I know there's an SDK for Dropbox API, but I need to scrape the website..
Thanks in advance, Amit.
Scripting/scraping the Dropbox web site is a violation of the Dropbox terms. Regardless, I wouldn't recommend doing so anyway, as it's subject to change without notice and so your code would be likely to break.
Dropbox does offer an extensive API though, which hopefully offers whatever functionality you're looking for, as well as an official Python SDK. I recommend using that instead.
If the API doesn't offer what you need, please let us know and we'll log it as a feature request.