Update Facebook Page's status using pyfacebook
I am attempting to add functionality to my Django app: when a new post is approved, I want to update the corresponding Facebook Page's status with a message and a link to the post automatically. Basic status update.
I have downloaded and installed pyfacebook - and I have read through the tutorial from Facebook. I have also seen this suggestion here on SO:
import facebook
fb = facebook.Facebook('YOUR_API_KEY', 'YOUR_SECRET_KEY')
fb.auth.createToken()
fb.login() # THIS IS AS FAR AS I CAN GET
fb.auth.getSession()
fb.set_status('Checking out StackOverFlow.com')
When I get to the login()
call, however, pyfacebook tries to open lynx so I can login to Facebook 'via the web' -- this is, obviously, not going to work for me because the system is supposed to be automated ...
I've been looking, but can't find out how I can keep this all working with the script and not having to lo开发者_开发百科gin via a web browser.
Any ideas?
In the definition of login
, particularly in the docstring, it appears as though the intended behavior is to open up a browser in order to have you log in that way.
def login(self, popup=False):
"""Open a web browser telling the user to login to Facebook."""
import webbrowser
webbrowser.open(self.get_login_url(popup=popup))
Looking at the facebook page User:PyFacebook_Tutorial that you linked, it looks like the example with login
is a "Desktop Applications" example. You want to follow the "Web Applications" section. I'd encourage you to simply press ahead with the tutorial there.
If you want to login to your facebook profile page I have managed to do it with this script:
Save this file as fb_login.py and in the same folder create a file fb_test.html
I successfully logged in as you can prove by viewing on your browser the fb_test.html or searching for your name in the plain text.
Does anyone knows how to login with simple Authedication credentials and not with SECRET AND API key that you need to make application?
import urllib, urllib2, cookielib
user = 'put_your_mail_here'
passwd = 'put_your_password_here'
file = './fb_test.html'
url_login = "https://login.facebook.com/login.php?"
url_action = "https://login.facebook.com/login.php?login_attempt=1"
url_topic = "http://www.facebook.com/profile.php?id=___put_your_profile_Number_here"
url_index = "https://login.facebook.com/login.php?"
def login(user, password, url_action):
cj = cookielib.LWPCookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor())
urllib2.install_opener(opener)
opener.addheaders=[('Content-Type','application/x-www-form-urlencoded'),('Connection','keep-alive'),('User-Agent','Mozilla/5.0')]
params = urllib.urlencode({'action':url_action , 'referer':url_index, 'email':user, 'pass':passwd,
'loginTrue':"login"})
f = opener.open(url_action, params)
f.close()
f = opener.open(url_action, params)
f.close()
return opener
def get_source_code( opener, url_x ):
f = opener.open(url_x)
data = f.read()
print type(data)
f.close()
return data
def keep_log( data, file ):
f = open(file, 'w')
f.write(data)
f.close()
opener = login(user, passwd, url_action)
src_code = get_source_code(opener, url_topic)
keep_log(src_code, file)
print src_code
精彩评论