Question

I would to create a script launched by a cron job that periodically store data about my channel in a file. Those data are retrieved via YouTube Analytics API (not the ordinary YouTube API).

How can i achieve this result? Are service access compatible with those API?

Was it helpful?

Solution

If this is just a once-off - for your account - running on a server/desktop that you trust, I think the best mechanism will be to:

  1. Create an OAuth client ID for an "Installed application". Create a Google Cloud Console project, enable the API you need, register your application (under "APIs & Auth").
  2. Go through the installed application flow once manually to grant yourself a refresh token - this will require you opening a browser and clicking through a consent screen.
  3. Persist the refresh token.
  4. On subsequent unattended runs, use the refresh token to get a fresh access token to make calls on your behalf.

If you are using Python - and have installed the Google API Python Client, the following code will do steps 2 - 4 for you (assuming you have already done step 1 and saved a client_secrets.json file):

import httplib2
from oauth2client.file import Storage
from oauth2client.client import flow_from_clientsecrets
from oauth2client.tools import run
from apiclient.discovery import build

storage = Storage("/path/to/saved_user_creds.dat")
credentials = storage.get()
if credentials is None or credentials.invalid:
  credentials = run(flow_from_clientsecrets("/path/to/client_secrets.json", scope="https://www.googleapis.com/auth/yt-analytics.readonly"), storage)
http = credentials.authorize(httplib2.Http())

# Do your stuff - remember to pass the authenticated http object to execute methods
service = build("youtubeAnalytics", "v1")
result = service.object().method(name=value).execute(http=http)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top