I am trying to get a YouTube livestream concurrent viewers details via the Analytics API (source at the bottom of the post) but the API would return a simple HttpError 403 Forbidden response most of the time ; or sometimes it may return a response with empty rows when the start-date/end-date do not align with streaming period.
Censoring out the channel and video IDs:
<HttpError 403 when requestinghttps://youtubeanalytics.googleapis.com/v2/reports[...]returned "Forbidden". Details: "[{'message': 'Forbidden', 'domain': 'global', 'reason': 'forbidden'}]">The request is properly identified via OAUTH 2.0 - and there is no problem using any other queries on either the Data API or the Analytics & Reporting API.Also the stream is publicly available.
Request to the YouTube Analytics & Reports API was build and sent through the help of the python google libs with the following OAUTH scopes authorized for the corresponding channel(s).
The main request
def livestream_analytics(self, channel_id, video_id): token_file = self.get_token(channel_id) api = self.get_service(token_file) response = api.reports().query( ids='channel=='+ channel_id, # 'UCxxx' the YouTube channel ID startDate='YYYY-mm-dd', # e.g.: '2024-04-16'<<< the day the stream happened endDate='YYYY-mm-dd', # same as with startDate dimensions='livestreamPosition', metrics='averageConcurrentViewers,peakConcurrentViewers', filters='video=='+ video_id # 'xxx' from https://www.youtube.com/watch?v=xxx ).execute() # either 403 Forbidden or Empty resultsFor references: building the API
from google_auth_oauthlib.flow import InstalledAppFlowfrom googleapiclient.discovery import builddef get_service(self, api, version, token_file): InstalledAppFlow.from_client_secrets_file( self.conf['client'], # the json client-secret file scopes=self.conf['scopes'] # scopes described as below ) credentials = self.get_credentials(token_file) return build( api, # 'youtubeAnalytics', version, # 'v2', credentials=credentials )The used OAUTH 2.0 scopes:
youtube.readonlyyoutube.channel-memberships.creatoryt-analytics.readonlyyt-analytics-monetary.readonlyyoutubepartneryoutubepartner-channel-auditAlso tried to shift around the startDate and endDate outside of the day the streams happened but it would return empty rows in the response instead of the Forbidden error
{'kind': 'youtubeAnalytics#resultTable', 'columnHeaders': [ {'name': 'livestreamPosition', 'columnType': 'DIMENSION', 'dataType': 'INTEGER'}, {'name': 'averageConcurrentViewers', 'columnType': 'METRIC', 'dataType': 'INTEGER'}, {'name': 'peakConcurrentViewers', 'columnType': 'METRIC', 'dataType': 'INTEGER'}], 'rows': []}References:
Also confused by the fact that neither the averageConcurrentViewers and peakConcurrentViewers metrics nor the livestreamPosition dimension seem to appear in the corresponding documentation pages for languages other than English (verified for French, German and Japanese pages).
Wondering whether the endpoint is not supposed to be available or whether it requires special permissions/settings like the members in the Data API.