简体   繁体   中英

Cannot upload a file in python + selenium. Keep getting [{“error”:true,“error_msg”:“Authentication Failed”}]

I am badly stuck up and I'm not able to proceed any further. Trust me, I have looked all over the web to get a CONCRETE solution but all in vain! I have an application justcloud.com where I need to upload a file and verify if it's uploaded. After I login to this application and reach the page where I have the button to select a file to be uploaded, here is my code:

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers
import urllib2

register_openers()
fileToUpload = {'files[]':open('/home/pinku/Desktop/Test_Upload.odt', 'rb')}
datagen, headers = multipart_encode(fileToUpload)
url = "https://upload.backupgrid.net/add"
request = urllib2.Request(url, datagen, headers)
print urllib2.urlopen(request).read()

The error I keep getting every single time is [{"error":true,"error_msg":"Authentication Failed"}]

I know I just need to simulate the actual file upload process which is nothing but an HTTP POST request to the server which also includes some authentication that I need to overcome.

My assumption is that may be cookies can help me in resolving this authentication issue but I am not sure and I do not know how to include it in my python code. Any sample code will be helpful. I request anyone reading this to help me.

This is not my first time on stackoverflow when I have posted this question but I have not received much help. I am still giving it a shot. Thanks anyways...

The error message is telling you exactly what the problem is. You need to be logged into that website before you can POST a file to that URL.

You need to get a logged in session cookie somehow. Typically when you POST to a login form URL you will receive an http response that includes this cookie. Then every time you make another request you keep sending that cookie with each request so that the site is aware you are authenticated.

How would you login to backupgrid? Find the URL for that, login, grab the cookie from the response and then include it with your request.

This stackoverflow answer provides a good explanation of how to add cookies to the request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM