OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

YouTube livestream metrics query: Forbidden error or empty response

  • Thread starter Thread starter bg_cg
  • Start date Start date
B

bg_cg

Guest
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:

Code:
<HttpError 403 when requesting
https://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

Code:
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 results

For references: building the API

Code:
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

def 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:

Code:
youtube.readonly
youtube.channel-memberships.creator
yt-analytics.readonly
yt-analytics-monetary.readonly
youtubepartner
youtubepartner-channel-audit

Also 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

Code:
{'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.
<p>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 <code>HttpError 403 Forbidden</code> 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.</p>
<p>Censoring out the channel and video IDs:</p>
<pre class="lang-py prettyprint-override"><code><HttpError 403 when requesting
returned "Forbidden". Details: "[{'message': 'Forbidden', 'domain': 'global', 'reason': 'forbidden'}]">
</code></pre>
<p>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.</p>
<p>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).</p>
<p>The main request</p>
<pre class="lang-py prettyprint-override"><code>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
).execute()
# either 403 Forbidden or Empty results
</code></pre>
<p>For references: building the API</p>
<pre class="lang-py prettyprint-override"><code>from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

def 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
)
</code></pre>
<p>The used OAUTH 2.0 scopes:</p>
<pre><code>youtube.readonly
youtube.channel-memberships.creator
yt-analytics.readonly
yt-analytics-monetary.readonly
youtubepartner
youtubepartner-channel-audit
</code></pre>
<p>Also tried to shift around the <code>startDate</code> and <code>endDate</code> outside of the day the streams happened but it would return empty rows in the response instead of the Forbidden error</p>
<pre class="lang-py prettyprint-override"><code>{'kind': 'youtubeAnalytics#resultTable', 'columnHeaders': [
{'name': 'livestreamPosition', 'columnType': 'DIMENSION', 'dataType': 'INTEGER'},
{'name': 'averageConcurrentViewers', 'columnType': 'METRIC', 'dataType': 'INTEGER'},
{'name': 'peakConcurrentViewers', 'columnType': 'METRIC', 'dataType': 'INTEGER'}
], 'rows': []}
</code></pre>
<p>References:</p>
<ul>
<li><a href="https://developers.google.com/youtube/analytics/metrics#Concurrent_Viewers" rel="nofollow noreferrer">https://developers.google.com/youtube/analytics/metrics#Concurrent_Viewers</a></li>
</ul>
<p>Also confused by the fact that neither the <code>averageConcurrentViewers</code> and <code>peakConcurrentViewers</code> metrics nor the <code>livestreamPosition</code> dimension seem to appear in the corresponding documentation pages for languages other than English (verified for French, German and Japanese pages).</p>
<p>Wondering whether the endpoint is not supposed to be available or whether it requires special permissions/settings like the members in the Data API.</p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top