|
1 | 1 | #!/usr/bin/python
|
2 | 2 |
|
3 |
| -import httplib2 |
| 3 | +# This sample shows how to rate a video. |
| 4 | +# Sample usage: |
| 5 | +# python like_video.py --videoId=OE63BYWdqC4 --rating=like |
| 6 | + |
| 7 | +import argparse |
4 | 8 | import os
|
5 |
| -import sys |
| 9 | +import re |
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 | +CLIENT_SECRETS_FILE = 'client_secret.json' |
43 | 29 |
|
44 | 30 | # This OAuth 2.0 access scope allows for full read/write access to the
|
45 | 31 | # 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 |
| -def get_authenticated_service(args): |
51 |
| - flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, |
52 |
| - scope=YOUTUBE_READ_WRITE_SCOPE, |
53 |
| - message=MISSING_CLIENT_SECRETS_MESSAGE) |
54 |
| - |
55 |
| - storage = Storage("%s-oauth2.json" % sys.argv[0]) |
56 |
| - credentials = storage.get() |
57 |
| - |
58 |
| - if credentials is None or credentials.invalid: |
59 |
| - credentials = run_flow(flow, storage, args) |
60 |
| - |
61 |
| - return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, |
62 |
| - http=credentials.authorize(httplib2.Http())) |
63 |
| - |
64 |
| -# Add the video rating. This code sets the rating to "like," but you could |
65 |
| -# also support an additional option that supports values of "like" and |
66 |
| -# "dislike." |
67 |
| -def like_video(youtube, video_id): |
| 32 | +SCOPES = ['https://www.googleapis.com/auth/youtube.force-ssl'] |
| 33 | +API_SERVICE_NAME = 'youtube' |
| 34 | +API_VERSION = 'v3' |
| 35 | + |
| 36 | +RATINGS = ('like', 'dislike', 'none') |
| 37 | + |
| 38 | +# Authorize the request and store authorization credentials. |
| 39 | +def get_authenticated_service(): |
| 40 | + flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) |
| 41 | + credentials = flow.run_console() |
| 42 | + return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) |
| 43 | + |
| 44 | +# Add the video rating. This code sets the rating to 'like,' but you could |
| 45 | +# also support an additional option that supports values of 'like' and |
| 46 | +# 'dislike.' |
| 47 | +def like_video(youtube, args): |
68 | 48 | youtube.videos().rate(
|
69 |
| - id=video_id, |
70 |
| - rating="like" |
| 49 | + id=args.videoId, |
| 50 | + rating=args.rating |
71 | 51 | ).execute()
|
72 | 52 |
|
73 |
| -if __name__ == "__main__": |
74 |
| - argparser.add_argument("--videoid", default="L-oNKK1CrnU", |
75 |
| - help="ID of video to like.") |
76 |
| - args = argparser.parse_args() |
| 53 | +if __name__ == '__main__': |
| 54 | + parser = argparse.ArgumentParser() |
| 55 | + parser.add_argument('--videoId', default='OE63BYWdqC4', |
| 56 | + help='ID of video to like.') |
| 57 | + parser.add_argument('--rating', default='like', |
| 58 | + choices=RATINGS, |
| 59 | + help='Indicates whether the rating is "like", "dislike", or "none".') |
| 60 | + args = parser.parse_args() |
77 | 61 |
|
78 |
| - youtube = get_authenticated_service(args) |
| 62 | + youtube = get_authenticated_service() |
79 | 63 | try:
|
80 |
| - like_video(youtube, args.videoid) |
| 64 | + like_video(youtube, args) |
81 | 65 | except HttpError, e:
|
82 |
| - print "An HTTP error %d occurred:\n%s" % (e.resp.status, e.content) |
| 66 | + print 'An HTTP error %d occurred:\n%s' % (e.resp.status, e.content) |
83 | 67 | else:
|
84 |
| - print "%s has been liked." % args.videoid |
| 68 | + print ('The %s rating has been added for video ID %s.' % |
| 69 | + (args.rating, args.videoId)) |
0 commit comments