Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法使用fbchat登录facebook_Python 3.x - Fatal编程技术网

Python 3.x 无法使用fbchat登录facebook

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

我正在尝试创建一个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: ")) 

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