Le Nguyen The Dat bio photo

Le Nguyen The Dat

~ Data Science & Engineering

Twitter Facebook LinkedIn Github

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:


image

  • Generate your OAuth’s client_secret.json credentials at the APIs & auth > Credentials tab:


image


image

(Click Download JSON after done)

  • Get your content_owner_id and channel_id of choice at your Youtube’s dashboard (content_owner_id will be displayed in the url - in this case: thIsiSaSampLeContentOwnErID):


image


Firing your first ever API call (and also, generate your oauth2 credential file i.e youtubeAnalytics.dat):

$ 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
$ 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