Hello, colleagues!
We are currently facing an issue with accessing the necessary analytics data for our Twitch streams and would appreciate any assistance you can provide.
Our company conducts live streams across six channels on various platforms, including Twitch. To streamline our reporting processes and reduce human error, we aim to utilize the Twitch API to automatically retrieve information about our streams. However, the public API methods such as streams, games, users, and channels do not provide the required data for our archived streams, which is available on the dashboard at Creator Dashboard.
We need to export the following analytics data via the API:
- Stream views;
- Unique viewers;
- Watch time;
- Maximum number of viewers;
- Average number of viewers;
- Engaged viewers;
- Chat messages.
We can obtain information for points 1, 4, 5, and 7 from the Streams endpoint, but we are unable to retrieve data for the other analytics.
We have reached out to Twitch support regarding access to the Analytics endpoint, but their response was not helpful. We received a 404 error when attempting to access this endpoint. We are seeking guidance or experiences in obtaining the required data from alternative endpoints.
Any help or insights would be greatly appreciated.
Additionally, please find below the code that results in a 404 error when accessing the Analytics endpoint:
import requests
# Twitch API credentials (указываем напрямую в коде)
CLIENT_ID = 'ваш_client_id' # Замените на ваш Client ID
CLIENT_SECRET = 'ваш_client_secret' # Замените на ваш Client Secret
# Twitch API endpoints
AUTH_URL = 'https://id.twitch.tv/oauth2/token'
API_URL = 'https://api.twitch.tv/helix'
# Function to get access token
def get_access_token():
response = requests.post(AUTH_URL, data={
'client_id': CLIENT_ID,
'client_secret': CLIENT_SECRET,
'grant_type': 'client_credentials'
})
if response.status_code != 200:
print(f"Ошибка при получении токена: {response.status_code}")
print(response.json())
return None
return response.json().get('access_token')
# Function to get analytics for a video
def get_video_analytics(video_id):
headers = {
'Client-ID': CLIENT_ID,
'Authorization': f'Bearer {ACCESS_TOKEN}'
}
params = {
'type': 'overview_v2',
'video_id': video_id
}
# Выводим информацию о запросе
print("\nОтправляемый запрос:")
print(f"URL: {API_URL}/analytics")
print(f"Headers: {headers}")
print(f"Params: {params}")
# Отправляем запрос
response = requests.get(f'{API_URL}/analytics', headers=headers, params=params)
if response.status_code != 200:
print(f"Ошибка при запросе к /analytics: {response.status_code}")
print(response.json())
return None
return response.json()
# Main function
def main():
global ACCESS_TOKEN
ACCESS_TOKEN = get_access_token()
if not ACCESS_TOKEN:
print("Не удалось получить токен доступа.")
return
# Ввод ID видео
video_id = input("Введите ID видео: ")
# Получаем аналитику для видео
analytics_data = get_video_analytics(video_id)
if analytics_data:
print("\nАналитика для видео:")
print(analytics_data)
else:
print("Не удалось получить аналитику для видео.")
if __name__ == '__main__':
main()
Thank you in advance for your assistance.