import requests
import json
token = "token"
group_id = 1
h ='тест'
text = 'магия!!!'
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,
'caption': h,
'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,
'message': text,
'access_token':token,
'from_group': '1',
'v':'5.101'}).json()
if __name__ == '__main__':
main()
import requests
import json
token = "token"
group_id = 1
h ='тест'
text = 'магия!!!'
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,
'caption': h,
'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, ')
def save_r2():
save_result = requests.get('https://api.vk.com/method/photos.saveWallPhoto?', params={'access_token': token,
'group_id': group_id,
'caption': h,
'photo': upload_response2['photo'],
'server': upload_response2['server'],
'hash': upload_response2['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') }
file2 = {'file1': open('1234.jpg', 'rb')}
global upload_response
global upload_response2
upload_response = requests.post(upload_url, files=file).json()
upload_response2 = requests.post(upload_url, files=file2).json()
save_result = save_r()
save_result2 = save_r2()
print(save_result)
print(save_result2)
save_result = (save_result + save_result2)
result2 = requests.get('https://api.vk.com/method/wall.post?', params ={'attachments':save_result,
'owner_id':-group_id,
'message': text,
'access_token':token,
'from_group': '1',
'v':'5.101'}).json()
if __name__ == '__main__':
main()