Skip to content

Commit 8e2f9d2

Browse files
authored
Add files via upload
1 parent 193d235 commit 8e2f9d2

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

Readme.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# YouTube Analytics and Reporting API Guide
2+
3+
## Overview
4+
5+
This repository contains code examples and instructions on how to use YouTube Analytics and Reporting APIs to unlock insights from your YouTube channel data. The examples demonstrate how to set up API credentials using a service account and access key metrics like video views and custom reports.
6+
7+
## Setup
8+
9+
### Clone the Repository
10+
11+
```bash
12+
git clone https://github.com/your-username/YouTube-Analytics-and-Reporting-APIs.git
13+
```
14+
15+
### Navigate to the Project Directory
16+
17+
```bash
18+
cd YouTube-Analytics-and-Reporting-APIs
19+
```
20+
21+
### Install Required Libraries
22+
23+
```bash
24+
pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib
25+
```
26+
27+
### Set Environment Variable
28+
Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the path of your service account key:
29+
30+
```bash
31+
set GOOGLE_APPLICATION_CREDENTIALS=path\to\your\service-account-file.json
32+
```
33+
34+
## Usage
35+
36+
### Retrieve Video Views
37+
Run the Python script read.py to retrieve video views:
38+
39+
```bash
40+
python analyze.py
41+
```
42+
43+
### Create and Download Custom Reports
44+
Run the script to create and download custom reports:
45+
46+
```bash
47+
python report.py
48+
```

analyze.py

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from google.oauth2 import service_account
2+
import googleapiclient.discovery
3+
4+
# Define the scope and service account file
5+
SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"]
6+
SERVICE_ACCOUNT_FILE = "path/to/service_account.json"
7+
8+
# Authenticate using service account credentials
9+
credentials = service_account.Credentials.from_service_account_file(
10+
SERVICE_ACCOUNT_FILE, scopes=SCOPES
11+
)
12+
13+
# Build the API client
14+
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
15+
16+
# Retrieve video views
17+
request = youtube.videos().list(
18+
part="statistics",
19+
id="VIDEO_ID"
20+
)
21+
response = request.execute()
22+
23+
print(f"Video Views: {response['items'][0]['statistics']['viewCount']}")

report.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from google.oauth2 import service_account
2+
import googleapiclient.discovery
3+
4+
# Define the scope and service account file
5+
SCOPES = ["https://www.googleapis.com/auth/youtube.readonly"]
6+
SERVICE_ACCOUNT_FILE = "path/to/service_account.json"
7+
8+
# Authenticate using service account credentials
9+
credentials = service_account.Credentials.from_service_account_file(
10+
SERVICE_ACCOUNT_FILE, scopes=SCOPES
11+
)
12+
13+
# Build the API client
14+
youtube = googleapiclient.discovery.build("youtube", "v3", credentials=credentials)
15+
16+
# List available report types
17+
report_types = youtube.reporting().reportTypes().list().execute()
18+
for report_type in report_types['reportTypes']:
19+
print(f"Report Type ID: {report_type['id']} - Name: {report_type['name']}")
20+
21+
# Create a reporting job
22+
job = youtube.reporting().jobs().create(body={'reportTypeId': 'YOUR_REPORT_TYPE_ID'}).execute()
23+
24+
# Download the report
25+
report_file = youtube.reporting().media().download(jobId=job['id'], reportId='REPORT_ID').execute()
26+
with open('report.csv', 'wb') as f:
27+
f.write(report_file)

0 commit comments

Comments
 (0)