1

Through python I want to post a fb status using the urllib, urllib2, cookielib (without downloading modules). I would successfully log in, but then my code for posting a status would not work. I would inspect the element of the status message box and post box (for m.facebook) and the status message box 'name' is status and the 'value' of the post box is Post but it would not post a status when I would try. How can I make this work?

import urllib2, urllib, cookielib                                                                                                                        


class Acc:                                                                                                                        
    jar = cookielib.CookieJar()                                                                                                                        
    cookie = urllib2.HTTPCookieProcessor(jar)                                                                                                                               
    opener = urllib2.build_opener(cookie)                                                                                                                        

    def login(self):                                                                                                                        
        try:                                                                                                                        
            params = urllib.urlencode({'email':'user@hotmail.com','pass':'password','login':'Log+In'})                                                                                                                        
            req = urllib2.Request('http://m.facebook.com/login.php?m=m&refsrc=m.facebook.com%2F', params)                                                                                                                        
            res = self.opener.open(req)                                                                                                                        
            html = res.read()                                                                                                                        


        except urllib2.HTTPError, e:                                                                                                                        
            print e.msg                                                                                                                        
        except urllib2.URLError, e:                                                                                                                        
            print e.reason[1]                                                                                                                        
        return False                                                                                                                        

    def fetch(self,url):                                                                                                                        
        req = urllib2.Request(url,None)                                                                                                                        
        res = self.opener.open(req)                                                                                                                        
        return res.read(), res.headers.get("Set-Cookie")                                                                                                                        

bla = Acc()                                                                                                                        
bla.login()                                                                                                                        
webpage, session = bla.fetch("http://m.facebook.com/login.php?m=m&refsrc=m.facebook.com%2F")                                                                                                                        
if "Logout" in webpage:                                                                                                                        
    print("Logged in cuzzie")                                                                                                                        

print("Sending Post")                                                                                                                        
post = urllib.urlencode({'status':'message','Post':'Post'})                                                                                                                        
a = urllib2.Request("http://m.facebook.com/")                                                                                                                        
a.add_header("cookie", session)                                                                                                                        
g = urllib2.urlopen(a, post)                                                                                                                        
print("Post Sent")                                                                                                                        
user3818650
  • 581
  • 1
  • 7
  • 19

1 Answers1

2

There are many hidden attributes which are being sent like csid, fb_dtsg ...

If you are using chrome you can see the network tab in developer tools just after posting a status. A POST request will be sent to "https://m.facebook.com/a/home.php", in that you can look at "Form Data" to see what all fields are sent in the request. I think fb_dtsg and csid are required for every fb post which you need to take from the login response page.

  • 1
    Could you explain more of how to catch requests like this? – user3818650 Sep 20 '14 at 10:19
  • 2
    Simple steps if using chrome 0)Open the fb page with the post dialog box ready 1) Inspect Element 2)Go to Network tab 3)Press the clear button to clear earlier sessions and click on "Record Network log" 4)Then post something, look at the first request that has been sent. In the Headers tab scroll down to Form data to check what was the payload sent. Check this link https://developer.chrome.com/devtools/docs/network for deeper understanding. – Sainath Motlakunta Sep 20 '14 at 14:13
  • 2
    Check this too http://stackoverflow.com/questions/9163251/chrome-source-of-post-data – Sainath Motlakunta Sep 20 '14 at 14:20