In the past few days, I’ve been working on retrieving data from various Google Products with their APIs, I figured it would be helpful (at least for myself!) to write a few short tutorials about them.
For a start, I would do it for Youtube Analytics API since it seems to be commonly used these days.
Prerequisites: You will need python 2.7 and a few libraries:
$ apt-get install -y python2.7 python2.7-dev python-setuptools python-pip
$ pip install --upgrade pip httplib2 google-api-python-client
Authentication and Details: Below are a few steps you need to do beforehands:
- Create new Google project at google’s developers console
- Enable Youtube APIs at the
APIs & auth > APIs
tab:
- Generate your OAuth’s
client_secret.json
credentials at theAPIs & auth > Credentials
tab:
(Click Download JSON
after done)
- Get your
content_owner_id
andchannel_id
of choice at your Youtube’s dashboard (content_owner_id
will be displayed in the url - in this case:thIsiSaSampLeContentOwnErID
):
Firing your first ever API call (and also, generate your oauth2
credential file i.e youtubeAnalytics.dat
):
- In other to do this, we will use my version of Youtube API Sample file:
$ git clone https://github.com/lenguyenthedat/google-data-api-tutorials.git
$ cd google-data-api-tutorials # Make sure you have `client_secret.json` in google-data-api-tutorials
$ cd Youtube/
$ python yt_analytics_report.py --channel_id thIsiSaSampLeChanNeLID --content_owner_id thIsiSaSampLeContentOwnErID # you can also use --noauth_local_webserver if you run this on a remote server
-
It’s also adviseable that you have a look at Youtube Channel Reports API Page or Youtube Content Owner Reports API Page for the list of
Metrics
,Dimensions
, andFilters
to be used in your own reports. -
Sample result:
$ python yt_analytics_report.py --channel_id thIsiSaSampLeChanNeLID --content_owner_id thIsiSaSampLeContentOwnErID
Analytics Data for channel: thIsiSaSampLeChanNeLID
video views comments favoritesAdded favoritesRemoved likes dislikes shares
123testRW4w 230.0 0.0 0.0 0.0 0.0 0.0 0.0
123testv3Lc 229.0 0.0 0.0 0.0 1.0 0.0 0.0
123testGxx8 161.0 0.0 0.0 0.0 0.0 0.0 0.0
123testOmcI 133.0 0.0 0.0 0.0 0.0 0.0 0.0
123test5pLI 131.0 0.0 0.0 0.0 6.0 0.0 0.0
123testbswQ 118.0 0.0 0.0 0.0 0.0 0.0 1.0