我想直接通过Instagram发送。这是我的代码
from InstagramAPI import InstagramAPI
import re
username="********"
InstagramAPI = InstagramAPI(username, "***********")
InstagramAPI.login()
InstagramAPI.direct_message("hey", 5580539929)
但我收到此错误
Traceback (most recent call last):
File "C:/Users/yasin/PycharmProjects/untitled2/venv/Include/yas.py", line 17, in <module>
InstagramAPI.direct_message("hey", 5580539929)
AttributeError: 'InstagramAPI' object has no attribute 'direct_message'
我该怎么办?
您应该在direct_message中使用,但函数中的参数必须为direct_message(api,message,userid)或使用与python相同的instagram的api api源代码:
if type(recipients) != type([]):
recipients = [str(recipients)]
recipient_users = '"",""'.join(str(r) for r in recipients)
endpoint = 'direct_v2/threads/broadcast/text/'
boundary = self.uuid
bodies = [
{
'type' : 'form-data',
'name' : 'recipient_users',
'data' : '[["{}"]]'.format(recipient_users),
},
{
'type' : 'form-data',
'name' : 'client_context',
'data' : self.uuid,
},
{
'type' : 'form-data',
'name' : 'thread',
'data' : '["0"]',
},
{
'type' : 'form-data',
'name' : 'text',
'data' : text or '',
},
]
data = self.buildBody(bodies,boundary)
self.s.headers.update (
{
'User-Agent' : self.USER_AGENT,
'Proxy-Connection' : 'keep-alive',
'Connection': 'keep-alive',
'Accept': '*/*',
'Content-Type': 'multipart/form-data; boundary={}'.format(boundary),
'Accept-Language': 'en-en',
}
)
#self.SendRequest(endpoint,post=data) #overwrites 'Content-type' header and boundary is missed
response = self.s.post(self.API_URL + endpoint, data=data)
if response.status_code == 200:
self.LastResponse = response
self.LastJson = json.loads(response.text)
return True
else:
print ("Request return " + str(response.status_code) + " error!")
# for debugging
try:
self.LastResponse = response
self.LastJson = json.loads(response.text)
except:
pass
return False