Skip to content

Instantly share code, notes, and snippets.

@exwyezed
Last active March 2, 2016 17:36
Show Gist options
  • Select an option

  • Save exwyezed/7654e72a0a1af16a8d9a to your computer and use it in GitHub Desktop.

Select an option

Save exwyezed/7654e72a0a1af16a8d9a to your computer and use it in GitHub Desktop.
YouTube API: Hacer sub a un canal 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 <--
CHANNELID_TO_SUB = "UCm63xpddsiHDwVoFZ0c9ArA"
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 add_subscription(youtube, channel_id):
add_subscription_response = youtube.subscriptions().insert(
part='snippet',
body=dict(
snippet=dict(
resourceId=dict(
channelId=channel_id
)
)
)).execute()
return add_subscription_response["snippet"]["title"]
if __name__ == "__main__":
args = argparser.parse_args()
youtube = get_authenticated_service(args)
try:
channel_title = add_subscription(youtube, CHANNELID_TO_SUB)
except HttpError, e:
print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content)
else:
print "A subscription to '%s' was added." % channel_title
@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

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