Answer the question
In order to leave comments, you need to log in
Why is the photo in Vkontakte not loaded from the server?
I wrote a code that makes a post in VK on the group wall. From my home PC on the test site, everything works fine, authorization passes, I get a token, I upload a photo, I make a post on the wall.
But I uploaded the code to the server and everything is fine there except for uploading the photo, here I can’t understand why it doesn’t upload the photo from the server to VK.
The error occurs at the line:
r = requests.post(server['upload_url'], files={'photo': open(path_img, "rb")})
# логин в ВК, получение токена
token_userid = auth(settings.VK_USER_ID, 'wall,photos,groups,messages,offline', settings.VK_LOGIN, settings.VK_PASS)
vkapi = vk.API(settings.VK_USER_ID, settings.VK_LOGIN, settings.VK_PASS)
vkapi.access_token = token_userid[0]
# заливаем фото на сервер
server = vkapi.photos.getWallUploadServer(uid = settings.VK_USER_ID)
r = requests.post(server['upload_url'], files={'photo': open(path_img, "rb")})
params = {
'server': r.json()['server'],
'photo': r.json()['photo'],
'hash': r.json()['hash']
}
wallphoto = vkapi.photos.saveWallPhoto(**params)
# постим пост в групе ВК
params = {
'owner_id': "-%s" % settings.VK_OWNER_ID,
'message': text
}
params.update({'attachments': 'photo%s_%s' % (wallphoto[0]['owner_id'], wallphoto[0]['id'])})
vkapi.wall.post(**params)
Answer the question
In order to leave comments, you need to log in
In general, I solved the problem. Honestly, I didn’t quite understand what the difference was, but I googled how others solved similar problems.
Instead of passing data in this form in requests.post:
We do this:
data = {}
files = {'photo': (instance.img_name(), open(path_img, 'rb'))}
url = server['upload_url'].split('?')[0]
for key, value in urlparse.parse_qs(server['upload_url'].split('?')[1]).iteritems():
data[key] = value
r = requests.post(url, data, files=files)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question