D
D
Dmitry2015-02-20 14:21:41
Python
Dmitry, 2015-02-20 14:21:41

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

rather returns an empty array instead of photo data.
Full download code and post on the wall:
# логин в ВК, получение токена
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)

On the production server it gives an error:
100. One of the parameters specified was missing or invalid: photos_list is invalid. params = [{u'value': u'1', u'key': u'oauth'}, {u'value': u'photos.saveWallPhoto', u'key': u'method'}, { u'value': u'[]', u'key': u'photo'}, {u'value': u'5.20', u'key': u'v'}, {u'value': u'38fdgrt6gbe9da99ffd3543sd45tgd2cd0a4c43c895c084a3935da434152dfgd5ye8e882eda3ec010 ', u'key': u'access_token ' }, {u'value': u'1424430663 ', u'key': u'timestamp '}, {u'value': u'7a09456dcb20a78cffgfd56ga2c1387e ', u 'key': u'hash'}, {u'value': u'622930', u'key': u'server'
{"server":622930,"photo":"[{\"photo\":\"8fef98a5ad:y\",\"sizes\":,\"kid\":\"67031f3e9c0a0eeb531f9597371d940f\"}]"," hash":"12571bf9ca75d2ef452dc9f765632964"}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2015-02-21
@trec

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)

instance.img_name() - photo file name
url - received address from VK (address only, no parameters)
data - parameter dictionary in url

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question