如何使用Youtube JavaScript API获取YouTube用户的观看历史记录?

问题描述 投票:0回答:4

我能够使用 YouTube Javascript API 获取用户的搜索结果、播放列表。

如何使用 YouTube JavaScript API 获取用户的观看历史记录???

是否有任何 JavaScript API 可以获取用户的 YouTube 观看历史记录?

javascript youtube-api youtube-data-api
4个回答
36
投票

对于 V3 API,这似乎不再可能。 watchHistory 播放列表 ID 现在始终设置为“HL”。


17
投票

以下是一般程序:

  1. 获取给定用户频道的“观看历史”播放列表(因为频道 ID 是获取用户信息的关键)。请注意,这仅在用户通过 oAuth2 进行身份验证时才有效。
https://www.googleapis.com/youtube/v3/channels?part=contentDetails&mine=true&key={YOUR_API_KEY}

有了这个响应,应该有一个“watchHistory”播放列表...获取它并调用 playlistItems 端点:

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=HLTFxEo9ofKM2Siifxoy5V_A&key={YOUR_API_KEY}

不幸的是,许多用户现在通过 API 报告观看历史播放列表中存在错误:

https://code.google.com/p/gdata-issues/issues/detail?id=4642

所以您的里程可能会有所不同。


0
投票

以下是检索

Watch Later
播放列表内容的 Python 脚本。

请注意,

SAPISIDHASH
__Secure_3PSID
__Secure_3PAPISID
值必须更改,因为在网络浏览器上加载
Watch Later
网页
导致卷曲请求与相同的URL(和https:/ /www.youtube.com/youtubei/v1/browse 用于在网络浏览器
SAPISIDHASH
开发者工具选项卡中滚动前 100 个结果后获取
Network

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)

0
投票

Google 阻止了对观看历史记录的访问(通过 YouTube Data API v3):https://issuetracker.google.com/issues/35172816

© www.soinside.com 2019 - 2024. All rights reserved.