|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 |
| -import httplib2 |
| 3 | +# This code sample creates a private playlist in the authorizing user's |
| 4 | +# YouTube channel. |
| 5 | +# Usage: |
| 6 | +# python playlist_updates.py --title=<TITLE> --description=<DESCRIPTION> |
| 7 | + |
| 8 | +import argparse |
4 | 9 | import os
|
5 |
| -import sys |
6 | 10 |
|
7 |
| -from apiclient.discovery import build |
8 |
| -from apiclient.errors import HttpError |
9 |
| -from oauth2client.client import flow_from_clientsecrets |
10 |
| -from oauth2client.file import Storage |
11 |
| -from oauth2client.tools import argparser, run_flow |
| 11 | +import google.oauth2.credentials |
| 12 | +import google_auth_oauthlib.flow |
| 13 | +from googleapiclient.discovery import build |
| 14 | +from googleapiclient.errors import HttpError |
| 15 | +from google_auth_oauthlib.flow import InstalledAppFlow |
12 | 16 |
|
13 | 17 |
|
14 | 18 | # The CLIENT_SECRETS_FILE variable specifies the name of a file that contains
|
|
21 | 25 | # https://developers.google.com/youtube/v3/guides/authentication
|
22 | 26 | # For more information about the client_secrets.json file format, see:
|
23 | 27 | # https://developers.google.com/api-client-library/python/guide/aaa_client_secrets
|
24 |
| -CLIENT_SECRETS_FILE = "client_secrets.json" |
25 |
| - |
26 |
| -# This variable defines a message to display if the CLIENT_SECRETS_FILE is |
27 |
| -# missing. |
28 |
| -MISSING_CLIENT_SECRETS_MESSAGE = """ |
29 |
| -WARNING: Please configure OAuth 2.0 |
30 |
| -
|
31 |
| -To make this sample run you will need to populate the client_secrets.json file |
32 |
| -found at: |
33 |
| -
|
34 |
| - %s |
35 |
| -
|
36 |
| -with information from the {{ Cloud Console }} |
37 |
| -{{ https://cloud.google.com/console }} |
38 |
| -
|
39 |
| -For more information about the client_secrets.json file format, please visit: |
40 |
| -https://developers.google.com/api-client-library/python/guide/aaa_client_secrets |
41 |
| -""" % os.path.abspath(os.path.join(os.path.dirname(__file__), |
42 |
| - CLIENT_SECRETS_FILE)) |
| 28 | + |
| 29 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
43 | 30 |
|
44 | 31 | # This OAuth 2.0 access scope allows for full read/write access to the
|
45 | 32 | # authenticated user's account.
|
46 |
| -YOUTUBE_READ_WRITE_SCOPE = "https://www.googleapis.com/auth/youtube" |
47 |
| -YOUTUBE_API_SERVICE_NAME = "youtube" |
48 |
| -YOUTUBE_API_VERSION = "v3" |
49 |
| - |
50 |
| -flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, |
51 |
| - message=MISSING_CLIENT_SECRETS_MESSAGE, |
52 |
| - scope=YOUTUBE_READ_WRITE_SCOPE) |
53 |
| - |
54 |
| -storage = Storage("%s-oauth2.json" % sys.argv[0]) |
55 |
| -credentials = storage.get() |
56 |
| - |
57 |
| -if credentials is None or credentials.invalid: |
58 |
| - flags = argparser.parse_args() |
59 |
| - credentials = run_flow(flow, storage, flags) |
60 |
| - |
61 |
| -youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
62 |
| - http=credentials.authorize(httplib2.Http())) |
63 |
| - |
64 |
| -# This code creates a new, private playlist in the authorized user's channel. |
65 |
| -playlists_insert_response = youtube.playlists().insert( |
66 |
| - part="snippet,status", |
67 |
| - body=dict( |
| 33 | +SCOPES = ['https://www.googleapis.com/auth/youtube'] |
| 34 | +API_SERVICE_NAME = 'youtube' |
| 35 | +API_VERSION = 'v3' |
| 36 | + |
| 37 | +# Authorize the request and store authorization credentials. |
| 38 | +def get_authenticated_service(): |
| 39 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 40 | + credentials = flow.run_console() |
| 41 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
| 42 | + |
| 43 | +def add_playlist(youtube, args): |
| 44 | + |
| 45 | + body = dict( |
68 | 46 | snippet=dict(
|
69 |
| - title="Test Playlist", |
70 |
| - description="A private playlist created with the YouTube API v3" |
| 47 | + title=args.title, |
| 48 | + description=args.description |
71 | 49 | ),
|
72 | 50 | status=dict(
|
73 |
| - privacyStatus="private" |
74 |
| - ) |
75 |
| - ) |
76 |
| -).execute() |
77 |
| - |
78 |
| -print "New playlist id: %s" % playlists_insert_response["id"] |
| 51 | + privacyStatus='private' |
| 52 | + ) |
| 53 | + ) |
| 54 | + |
| 55 | + playlists_insert_response = youtube.playlists().insert( |
| 56 | + part='snippet,status', |
| 57 | + body=body |
| 58 | + ).execute() |
| 59 | + |
| 60 | + print 'New playlist ID: %s' % playlists_insert_response['id'] |
| 61 | + |
| 62 | +if __name__ == '__main__': |
| 63 | + |
| 64 | + parser = argparse.ArgumentParser() |
| 65 | + parser.add_argument('--title', |
| 66 | + default='Test Playlist', |
| 67 | + help='The title of the new playlist.') |
| 68 | + parser.add_argument('--description', |
| 69 | + default='A private playlist created with the YouTube Data API.', |
| 70 | + help='The description of the new playlist.') |
| 71 | + |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + youtube = get_authenticated_service() |
| 75 | + try: |
| 76 | + add_playlist(youtube, args) |
| 77 | + except HttpError, e: |
| 78 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
0 commit comments