Python 3.x 无法使用fbchat登录facebook
我正在尝试创建一个python程序来发送fb消息,但无法使用以下代码登录Python 3.x 无法使用fbchat登录facebook,python-3.x,Python 3.x,我正在尝试创建一个python程序来发送fb消息,但无法使用以下代码登录 import fbchat from getpass import getpass username = input("username: ") client = fbchat.Client(username, getpass()) print(client.getSessions) no_of_friends = 1 #int(("Number of friends: "))
import fbchat
from getpass import getpass
username = input("username: ")
client = fbchat.Client(username, getpass())
print(client.getSessions)
no_of_friends = 1 #int(("Number of friends: "))
for i in range(no_of_friends):
name = str(input("Name: "))
friends = client.searchForUsers(name) # return a list of names
friend = friends[0]
msg = input("Message: ")
sent = client.send(fbchat.models.Message( msg), thread_id = int(friend.uid))
if sent:
print("Message sent successfully!")
它给出了以下错误
Attempt #1 failed, retrying
Traceback (most recent call last):
File "/usr/local/lib/python3.8/dist-packages/fbchat/_client.py", line 205, in login
self._state = State.login(
File "/usr/local/lib/python3.8/dist-packages/fbchat/_state.py", line 151, in login
return cls.from_session(session=session)
File "/usr/local/lib/python3.8/dist-packages/fbchat/_state.py", line 190, in from_session
revision = int(r.text.split('"client_revision":', 1)[1].split(",", 1)[0])
IndexError: list index out of range
试试这个,我已经测试过了,效果非常好。你需要添加用户代理
这听起来像是该库维护人员的错误报告……如何使用Python fbchat使用messenger在Facebook上发送消息?
import fbchat
import re
fbchat._util.USER_AGENTS = ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36"]
fbchat._state.FB_DTSG_REGEX = re.compile(r'"name":"fb_dtsg","value":"(.*?)"')
from getpass import getpass
username = str(input("Username: "))
client = fbchat.Client(username, getpass())
no_of_friends = int(input("Number of friends: "))
for i in range(no_of_friends):
name = str(input("Name: "))
friends = client.searchForUsers(name) # return a list of names
friend = friends[0]
msg = str(input("Message: "))
sent = client.send(fbchat.models.Message( msg), thread_id = int(friend.uid))
if sent:
print("Message sent successfully!")