Last active
March 2, 2016 17:36
-
-
Save exwyezed/7654e72a0a1af16a8d9a to your computer and use it in GitHub Desktop.
YouTube API: Hacer sub a un canal con script de Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Para utilizar el script necesitas tener instalada la siguiente librería:
pip install --upgrade google-api-python-client