使用 pyfacebook 更新 Facebook 页面的状态

发布于 2024-08-15 02:44:28 字数 876 浏览 5 评论 0原文

我正在尝试向我的 Django 应用程序添加功能:当新帖子获得批准时,我想使用消息和帖子链接自动更新相应的 Facebook 页面的状态。基本状态更新。

我已经下载并安装了 pyfacebook - 并且我已通读来自 Facebook 的教程。我也看到了这个建议这里

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')

当我到达然而,login() 调用,pyfacebook 尝试打开 lynx,以便我可以“通过网络”登录 Facebook——显然,这对我不起作用,因为系统应该自动化... 我一直在寻找,但不知道如何让这一切与脚本一起工作,而不必通过网络浏览器登录。

有什么想法吗?

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 login via a web browser.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别再吹冷风 2024-08-22 02:44:28

login 的定义中,特别是在文档字符串中,似乎预期的行为是打开浏览器以便让您以这种方式登录。

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))

查看您链接的 Facebook 页面 User:PyFacebook_Tutorial,看起来就像 login 的示例是一个“桌面应用程序”示例。您想要遵循“Web 应用程序”部分。我鼓励您继续阅读那里的教程。

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.

暗藏城府 2024-08-22 02:44:28

如果您想登录您的 Facebook 个人资料页面,我已经设法使用此脚本来完成此操作:

将此文件另存为 fb_login.py 并在同一文件夹中创建一个文件 fb_test.html

我已成功登录,您可以通过在浏览器上查看 fb_test.html 或在纯文本中搜索您的姓名来证明。

有谁知道如何使用简单的身份验证凭据登录,而不是使用申请所需的秘密和 API 密钥登录?

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文