Skip to content

Instantly share code, notes, and snippets.

@exwyezed
Last active February 25, 2021 22:12
Show Gist options
  • Select an option

  • Save exwyezed/1e9ddda14afb4794c1bf to your computer and use it in GitHub Desktop.

Select an option

Save exwyezed/1e9ddda14afb4794c1bf to your computer and use it in GitHub Desktop.
YouTube API: Puntuar vídeo con script de Python
#!/usr/bin/python
import httplib2
import sys
from apiclient.discovery import build
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
# 1. Entra en https://console.developers.google.com/projectselector/home/dashboard
# 2. Crea un project nuevo
# 3. Habilita la API de YT "YouTube Data API" para el project
# 4. En la columna izq haz click en la tab "Credenciales"
# 5. Usa la option "ID de cliente OAuth" para crear las credenciales
# 6. Descarga las credenciales en un archivo JSON y muevelo junto a este script
CLIENT_SECRETS_FILE = "credenciales.json"
# --> VARIABLES <--
VIDEOID_TO_RATE = "L-oNKK1CrnU"
RATE_OPTION = "like" # ["like", "dislike"]
YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE,
scope=YOUTUBE_READ_WRITE_SCOPE,
message="Archivo credenciales.json no encontrado")
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def like_video(youtube, video_id):
youtube.videos().rate(
id=VIDEOID_TO_RATE,
rating=RATE_OPTION
).execute()
if __name__ == "__main__":
args = argparser.parse_args()
youtube = get_authenticated_service(args)
try:
like_video(youtube, VIDEOID_TO_RATE)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
else:
print "ID: #" + VIDEOID_TO_RATE + " has been " + RATE_OPTION + 'd.'
@exwyezed

exwyezed commented Mar 2, 2016

Copy link
Copy Markdown
Author

Para utilizar el script necesitas tener instalada la siguiente librería: pip install --upgrade google-api-python-client

@jansgreen

Copy link
Copy Markdown

Literalmente lo que hiciste es copiar los codigo de ejemplo de google API y lo pegaste, que solucion aportas? ami no me funciona y estoy buscando una solucion, y simpremente copias y pega, da una solucion detallada como por ejemplo, como manipular Storage, para que sirve que debo de hacer para que me funcione correctamente https://developers.google.com/youtube/v3/code_samples/python#rate__like__a_video

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment