S
S
Sergey Sergey2020-06-05 20:13:23
Python
Sergey Sergey, 2020-06-05 20:13:23

How to attach video from youtube vk api?

Hello everyone, please tell me how to upload on the wall not a link to a video, but so that the video is attached through the api, when you just insert a link through the VK site, the vidos itself is pulled up, but not through the api.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Karbivnichy, 2020-06-05
@hottabxp

Api vk did not work with the video, but ran through the docks (without reading). The algorithm is approximately the following:
1) Call the video.save method - pass the url of the video from youtube, token and some other parameters. In case of successful execution of the method, you get json with video and video_id parameters (you need to pass it to the next method).
2) Call the video.add method - pass in it the id of the user or community where to add the video (in this case, your id), video_id and the id of the video owner (as I understand it, also your id). If successful, the method will return 1.
Look at the documentation, there are some interesting optional parameters.
Example:

import requests
import json

token = '' # Токен, с доступом к видео
my_id = ''	# Свой id

def video_save(link):

  response = requests.get(f'https://api.vk.com/method/video.save?link={link}&access_token={token}&v=5.107')
  # print(response.text) # мощная функция для отладки кода :)
  j_data = json.loads(response.text) # Возвращает адрес сервера, необходимый для загрузки, и данные видеозаписи.
  return j_data['response']['video_id'] # Возвращаем из функции только video_id(для удобства)

def video_add(target_id, video_id, owner_id):
  # target_id - id пользователя или сообщества, в которое нужно добавить видео.
  # video_id - id видеозаписи.
  # owner_id -id пользователя или сообщества, которому принадлежит видеозапись.
  response = requests.get(f'https://api.vk.com/method/video.add?target_id={target_id}&owner_id={owner_id}&video_id={video_id}&access_token={token}&v=5.107')
  return response.text # Если вернет 1, значит видео добавлено


video_id = video_save('https://www.youtube.com/watch?v=58RSC7HO9aU') # Получаем video_id
result = video_add(my_id,video_id,my_id) # Добавляем видео себе в видеозаписи
print(result)

Smoke api docks carefully, VK has one of the simplest apis!

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question