Play with YouTube Data API v3 using Google API Python Client

App registration

Turn on the YouTube Data API.

https://console.developers.google.com/project

Install google-api-python-client

pip install --upgrade google-api-python-client

https://github.com/google/google-api-python-client

doc

https://developers.google.com/youtube/v3/docs/channels/list

Official sample code

sample: https://developers.google.com/youtube/v3/code_samples/python?hl=ja

Completion code

Extracted from Django's views.py

from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
 

from oauth2client.client import OAuth2WebServerFlow
import httplib2
from apiclient.discovery import build
 
CLIENT_ID = 'xxxxx'
CLIENT_SECRET = 'xxxxx'
FLOW = OAuth2WebServerFlow(
    client_id=CLIENT_ID,
    client_secret=CLIENT_SECRET,
    scope='https://www.googleapis.com/auth/youtube',
    redirect_uri='http://localhost:8000/account/fetch_channel/on_auth/'
)
 
 
def index_view(request):
    return render(request, 'index.html', {})
 
 
def fetch_channel(request):
    auth_uri = FLOW.step1_get_authorize_url()
    return HttpResponseRedirect(auth_uri)
 
 
def fetch_channel_on_auth(request):
    code = request.GET.get('code', None)
    credentials = FLOW.step2_exchange(code)
    http = httplib2.Http()
    http = credentials.authorize(http)
    service = build('youtube', 'v3', http=http)
    r = service.channels().list(
        mine=True,
        part="snippet"
    ).execute()
    username = r['items'][0]['snippet']['title']
    return HttpResponse(username)

Recommended Posts

Play with YouTube Data API v3 using Google API Python Client
Get data from analytics API with Google API Client for python
I tried using YOUTUBE Data API V3
Get Youtube data in Python using Youtube Data API
Creating Google Spreadsheet using Python / Google Data API
[Python] Get all comments using Youtube Data API
Get Youtube data with python
Play RocketChat with API / Python
[Python] Get insight data using Google My Business API
Speech file recognition by Google Speech API v2 using Python
Data acquisition from analytics API with Google API Client for python Part 2 Web application
Data acquisition using python googlemap api
[Python3] Google translate google translate without using api
[Python] I tried to get various information using YouTube Data API!
Simple Slack API client made with Python
Try using Dropbox API v2 with Go
Try using Python with Google Cloud Functions
Play with puns using the COTOHA API
Play with 2016-Python
Play with Google Spread Sheets in python (OAuth)
Run Google Analytics API (core v3) in python
Get stock price data with Quandl API [Python]
Recent ranking creation using Qiita API with Python
[Google Cloud Platform] Use Google Cloud API using API Client Library
How to use Service Account OAuth and API with Google API Client for python
Data analysis with python 2
Data analysis using Python 0
Play youtube in python
Data cleaning using Python
Data analysis with Python
Play audio files from Python with interrupts
Play with puns using the COTOHA API
Play with Prophet
Play with PyTorch
Play with 2016-Python
Play with CentOS 8
Play with Pyramid
Play with Fathom
Upload files with Django
Play with Othello (Reversi)
Discord Bot with recording function starting with Python: (4) Play music files
Play with YouTube Data API v3 using Google API Python Client
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Get LEAD data using Marketo's REST API in Python
Easy way to scrape with python using Google Colab
Register a ticket with redmine API using python requests
Get comments and subscribers with the YouTube Data API
Process csv data with python (count processing using pandas)
How to analyze with Google Colaboratory using Kaggle API
Speech transcription procedure using Python and Google Cloud Speech API
Upload to a shared drive with Google Drive API V3
I tried hitting the API with echonest's python client
Upload as open data using CKAN API in Python & automatically link with Github Actions
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
Sample data created with python
Use Trello API with python
I tried to search videos using Youtube Data API (beginner)
[S3] CRUD with S3 using Python [Python]
Automatically organized mp3s downloaded from Google Play Music using Python
Use Twitter API with Python
Using Quaternion with Python ~ numpy-quaternion ~
YouTube video management with Python 3
Study Python with Google Colaboratory
Inflating text data by retranslation using google translate in Python
[Python] Using OpenCV with Python (Basic)
Upload videos using YouTube API
[Treasure Data] [Python] Execute a query on Treasure Data using TD Client
Web API with Python + Falcon
[Python] Play with Discord's Webhook.
[Introduction] Artificial satellite data analysis using Python (Google Colab environment)
Read English sentences by hitting Google Translate API with Python without using the distributed module
[Python] I tried collecting data using the API of wikipedia