L
L
lsnowl2020-12-04 17:45:23
Python
lsnowl, 2020-12-04 17:45:23

Json.decoder.JSONDecodeError , I can't upload more than 2 pictures for a post to the VK group (vk api). How to be?

I want to make an auto-poster bot for the VK group ...
Below is the program code, I marked with a comment where the error is

def write_json(data,filename):
  with open(filename,'w') as file:
    json.dump(data,file,indent=2,ensure_ascii=False)

def get_upload_server():
  r=requests.get('https://api.vk.com/method/photos.getWallUploadServer',params={
    'access_token':token,
    'group_id':GROUP_ID,
    'v':ver,
    }).json()
  return r['response']['upload_url']
  write_json(r,'upload_response.json')

def main():
  upload_url=get_upload_server()
     #ЗДЕСЬ ОШИБКА. Если файлов будет 2 или 1, то всё работает, если больше 2, то выдает ошибку
  files={
  'file1': open('IPAD1X.png','rb'),
  'file2':open('Layer61X.png','rb'),
  'file3':open('IphoneX.png','rb')}

  upload_response=requests.post(upload_url,files=files).json()
  write_json(upload_response,'upload_response.json')
  result=requests.get('https://api.vk.com/method/photos.saveWallPhoto',params={
    'access_token':token,
    'group_id':GROUP_ID,
    'server':upload_response['server'],
    'photo':upload_response['photo'],
    'hash':upload_response['hash'],
    'v':ver,}).json()

When 2 pictures:
5fca4b5d4154e718909494.png
When 3 pictures:
5fca48dc2e465621815821.png
I've been trying to fix it for 2 days, no ideas :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-12-04
@lsnowl

I've been trying to fix it for 2 days

And if you try to just print response.text, without converting it to json?
Let's see what's there
414 Request-URI Too Long

Which means that there are a lot of characters in the URL (well, in principle, it is logical, because three images are 3500+ characters long)
So it would be more reasonable to replace get with post and get
result=requests.post('https://api.vk.com/method/photos.saveWallPhoto', data={
    'access_token':token,
    'group_id':GROUP_ID,
    'server':upload_response['server'],
    'photo':upload_response['photo'],
    'hash':upload_response['hash'],
    'v':ver,}).json()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question