I am trying to use google cloud to get the stats of my youtube channel, but I don't want to have to complete an OAuth every time and enter key into console.. I have an API key and am wondering if its possible to just get the Stats using the API key instead of having to go through OAuth process, as I plan to hook this up to a display and don't want to constantly have to do the OAuth.
Here is the code I copied from the Github, but once again, this does the OAuth and its kinda a pain and I wish to just use API key so I don't have to interact with it to get my stats.
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 = 'YTClientSecrets.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)
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'
youtubeAnalytics = get_service()
execute_api_request(
youtubeAnalytics.reports().query,
ids='channel==MINE',
startDate='2017-01-01',
endDate='2021-12-31',
metrics='estimatedMinutesWatched,views,likes,subscribersGained',
dimensions='day',
sort='day'
)
print(response.text)```
Answer: no you can not use an API key to access private user data.
If you check the documentation for jobs.reports.get you will notice the following
All methods which request private user data will have an authorization section. This tells you which authorization scope is required in order to access the private data.
API keys only allow you to access public data, for exampmle public videos uploaded to youtube are public data so you do not need to be authorized to access them you can use an api key.
There is another type of authorization called service account authorization which allows you to pre authorize a service account to access private user data.
However there are limitations to which APIs support service account authentication.
If you can not use Service account Authentication your only option is to make a single user type system where you authorize your script store your refresh token so that your script can use the refresh token in the future to request a new access token.