How to get watched history of YouTube user using Youtube JavaScript API?
Asked Answered
B

4

21

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

Barns answered 15/6, 2015 at 15:37 Comment(1)
This is no longer possible, see this 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 answerYen
C
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.

Copenhagen answered 15/6, 2015 at 15:58 Comment(3)
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); }); }Barns
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..Barns
@Barns 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...Griffin
R
37

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

Rowell answered 27/7, 2017 at 21:52 Comment(1)
It looks like Google blocked access to the watch history: issuetracker.google.com/issues/35172816.Mistakable
C
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.

Copenhagen answered 15/6, 2015 at 15:58 Comment(3)
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); }); }Barns
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..Barns
@Barns 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...Griffin
M
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)
Meatiness answered 16/6, 2023 at 21:39 Comment(0)
P
0

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

Pyles answered 29/8, 2023 at 12:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.