S
S
scyfox2019-07-06 11:27:41
Python
scyfox, 2019-07-06 11:27:41

How to upload a photo to a VK community wall using the python API?

I have been struggling with this issue for several weeks now. I already have code that should work, but there is one problem:

import requests
import json

token = #здесь токен
group_id =#здесь id группы


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

def save_r():
      save_result = requests.get('https://api.vk.com/method/photos.saveWallPhoto?', params ={'access_token':token,
                                            'group_id':group_id,
                                            'photo':upload_response['photo'],
                                            'server':upload_response['server'],
                                            'hash':upload_response['hash'],
                                            'v':'5.101'}).json()
      return ('photo'+str(save_result['response'][0]['owner_id'])+'_'+str(save_result['response'][0]['id'])+'&access_key='+str(save_result['response'][0]['access_key']))

def main():
  upload_url = getWallUploadServer()
  file = {'file1': open('123.jpg', 'rb') }
  global upload_response
  upload_response = requests.post(upload_url, files=file).json()	
  save_result = save_r()
  result2 = requests.get('https://api.vk.com/method/wall.post?', params ={'attachments':save_result,
                                      'owner_id-':group_id,
                                      'access_token':token,
                                      'from_group': '1',
                                      'v':'5.101'}).json()

if __name__ == '__main__':
  main()

This code attaches the picture not to the group, but to my wall! Although all the parameters seem to be set correctly (
Please help

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question