I am able to get search results, playlists of an user using YouTube Javascript API.
How can I get the watched history of an user using YouTube JavaScript API???
Is there any JavaScript API to get the YouTube watched history of an user??
I am able to get search results, playlists of an user using YouTube Javascript API.
How can I get the watched history of an user using YouTube JavaScript API???
Is there any JavaScript API to get the YouTube watched history of an user??
This no longer seems possible with the V3 API. The watchHistory playlist ID is now always set to 'HL'.
Here's the general procedure:
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&key={YOUR_API_KEY}
With that response, there should be a "watchHistory" playlist ... take it and call the playlistItems endpoint:
https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=HLTFxEo9ofKM2Siifxoy5V_A&key={YOUR_API_KEY}
Unfortunately, many users are reporting there's a bug in the watch history playlist through the API right now:
https://code.google.com/p/gdata-issues/issues/detail?id=4642
So your mileage may vary.
The following is a Python script retrieving Watch Later
playlist content.
Note that SAPISIDHASH
, __Secure_3PSID
and __Secure_3PAPISID
values have to be changed thanks to loading on your web-browser the Watch Later
webpage resulting in a curl request interacting with the same URL (and https://www.youtube.com/youtubei/v1/browse for getting SAPISIDHASH
after having scrolled the first 100 results) in your web browser Network
developer tools tab.
import requests, json
url = 'https://www.youtube.com/playlist?list=WL'
__Secure_3PSID = '__Secure_3PSID.'
# Note that `__Secure_3PAPISID`, `headers`, `cookies` and `context` aren't needed for just listing first 100 videos from `Watch Later` playlist.
__Secure_3PAPISID = '__Secure_3PAPISID'
SAPISIDHASH = 'SAPISIDHASH'
headers = {
'Content-Type': 'application/json',
'Origin': 'https://www.youtube.com',
'Authorization': f'SAPISIDHASH {SAPISIDHASH}'
}
cookies = {
'__Secure-3PSID': __Secure_3PSID,
'__Secure-3PAPISID': __Secure_3PAPISID
}
context = {
'client': {
'clientName': 'WEB',
'clientVersion': '2.20230613.01.00'
}
}
content = requests.get(url, cookies = cookies).text
content = content.split('">var ytInitialData = ')[1].split(';</script>')[0]
data = json.loads(content)
def treatContents(contents):
MAX_RESULTS = 100
for content in contents[:MAX_RESULTS]:
playlistVideoRenderer = content['playlistVideoRenderer']
videoId = playlistVideoRenderer['videoId']
title = playlistVideoRenderer['title']['runs'][0]['text']
print(videoId, title)
# If response contains a `token` for retrieving additional pages, let's continue.
if len(contents) > MAX_RESULTS:
token = contents[MAX_RESULTS]['continuationItemRenderer']['continuationEndpoint']['continuationCommand']['token']
url = 'https://www.youtube.com/youtubei/v1/browse'
data = {
'context': context,
'continuation': token
}
data = requests.post(url, headers = headers, cookies = cookies, json = data).json()
continuationItems = data['onResponseReceivedActions'][0]['appendContinuationItemsAction']['continuationItems']
treatContents(continuationItems)
contents = data['contents']['twoColumnBrowseResultsRenderer']['tabs'][0]['tabRenderer']['content']['sectionListRenderer']['contents'][0]['itemSectionRenderer']['contents'][0]['playlistVideoListRenderer']['contents']
treatContents(contents)
Google blocked access to the watch history (via Youtube Data API v3): issuetracker.google.com/issues/35172816.