去年,我能够通过 Playlistitems > list 检索所有状态的视频(通过 OAuth 进行身份验证)指定包含所有上传的播放列表。
截至今天,相同的代码只给我公开上传:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "CLIENT_SECRETS.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.playlistItems().list(part="snippet", maxResults=50, playlistId="__PLAYLIST__ID")
response = request.execute()
total = response['pageInfo']['totalResults']
print("total number of videos is " + str(total));
exit();
if __name__ == "__main__":
main()
我知道其他人已经问过这个问题,但它们要么是很老的帖子,要么没有得到全面的回答。唯一其他可能的方法似乎是 Search > list指定 channel ID,但这也只返回公共(public)视频,尽管通过 OAuth 进行身份验证:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "YOUR_CLIENT_SECRET_FILE.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube.search().list(
part="snippet",
channelId="___CHANNEL__ID___",
type="video"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
文档 claims设置 forMine
范围:[...] can only be used in a properly authorized request. The
forMine
parameter restricts the search to only retrieve videos owned by the authenticated user. If you set this parameter totrue
, then thetype
parameter's value must also be set tovideo
.
但是,当我包含
forMine
时,这会导致请求无效。设置为 true
.关于如何 的任何建议目前获取私有(private)/未列出的 YouTube 视频数据将不胜感激。
最佳答案
我会not rely much在 totalResults
;该属性的规范并不可靠(以下重点是我的):
pageInfo.totalResults (integer)
The total number of results in the result set. Please note that the value is an approximation and may not represent an exact value. In addition, the maximum value is 1,000,000.
You should not use this value to create pagination links. Instead, use the
nextPageToken
andprevPageToken
property values to determine whether to show pagination links.
如果只需要给定播放列表的项目数,由其 ID
PLAYLIST_ID
标识,我建议查询 Playlists.list
带有 id=PLAYLIST_ID
的 API 端点寻找房产 contentDetails.itemCount
.但是,否则,如果在分页时(使用
PlaylistItems.list
端点),API 不会为您提供所有私有(private)和未列出的视频(假设您向端点传递了一个有效的访问 token ),那么很可能这是一个错误needs to be reported 的 API .另外,请注意,根据 YouTube's staff ,通过
PlaylistItems.list
返回的项目数的上限为 20000。端点(注意:设计本身会随着时间而改变:据我所知,不久前这个限制还不存在)。
关于Youtube Data API - 如何获取我的私有(private)和未列出的视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64185792/