20

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??

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
user3062437
  • 347
  • 1
  • 2
  • 14
  • 4
    This is no longer possible, see [this](https://stackoverflow.com/questions/46987690/tracking-youtube-watch-history) answer. Google deprecated the ability to see watch history and watch later. Wanted comment on this ticket as I searched around for a little while to find this answer – Mike Kellogg Apr 22 '18 at 02:11

4 Answers4

36

This no longer seems possible with the V3 API. The watchHistory playlist ID is now always set to 'HL'.

Ronald Currier
  • 575
  • 1
  • 5
  • 11
17

Here's the general procedure:

  1. Get the "watch History" playlist of a given user's channel (as a channel ID is the key to getting user info). Note that this will ONLY work when a user is authenticated via oAuth2.
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.

Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
  • Thanks For your help!!! I have got the watched history of a particular user from following code: function handleAPILoaded() { var request = gapi.client.youtube.channels.list({ mine: true, part: 'contentDetails' }); request.execute(function (response) { debugger; playlistId = response.result.items[0].contentDetails.relatedPlaylists.watchHistory; requestVideoPlaylist(playlistId); }); } – user3062437 Jun 30 '15 at 10:35
  • And i need one more help...How can i get the categories of watched videos from watchHistory.. I want the category based watched videos.. I will be more helpfull for me I you give answer for this query. I thank you for your help.. – user3062437 Jun 30 '15 at 10:40
  • 2
    @user3062437 In my case, the request what you suggested to get playlist id for watch history returned wrong playlist id ("HL"). Of course, when send api for detail with the wrong playlist id("HL") returned empty list. Google work for us plz... – Brad Hong Oct 05 '16 at 08:14
0

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)
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
0

Google blocked access to the watch history (via Youtube Data API v3): issuetracker.google.com/issues/35172816.

Kirill I.
  • 11
  • 2