-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add .py sample for calling YT Analytics API (v2)
- Loading branch information
1 parent
cdf1f87
commit 0726330
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import google.oauth2.credentials | ||
import google_auth_oauthlib.flow | ||
from googleapiclient.discovery import build | ||
from googleapiclient.errors import HttpError | ||
from google_auth_oauthlib.flow import InstalledAppFlow | ||
|
||
SCOPES = ['https://www.googleapis.com/auth/yt-analytics.readonly'] | ||
|
||
API_SERVICE_NAME = 'youtubeAnalytics' | ||
API_VERSION = 'v2' | ||
CLIENT_SECRETS_FILE = 'YOUR_CLIENT_SECRET_FILE.json' | ||
def get_service(): | ||
flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRETS_FILE, SCOPES) | ||
credentials = flow.run_console() | ||
return build(API_SERVICE_NAME, API_VERSION, credentials = credentials) | ||
|
||
def execute_api_request(client_library_function, **kwargs): | ||
response = client_library_function( | ||
**kwargs | ||
).execute() | ||
|
||
print(response) | ||
|
||
if __name__ == '__main__': | ||
# Disable OAuthlib's HTTPs verification when running locally. | ||
# *DO NOT* leave this option enabled when running in production. | ||
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1' | ||
|
||
youtubeAnalytics = get_service() | ||
execute_api_request( | ||
youtubeAnalytics.reports().query, | ||
ids='channel==MINE', | ||
startDate='2017-01-01', | ||
endDate='2017-12-31', | ||
metrics='estimatedMinutesWatched,views,likes,subscribersGained' | ||
dimensions='day', | ||
sort='day' | ||
) |
0726330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good