我试图把我在 https://developers.google.com/youtube/v3/docs 中发现的三个单独的代码放在一起作为示例:在我的频道中创建播放列表 (https://developers.google.com/youtube/v3/docs/playlists/insert)搜索标题中包含给定关键字的视频(在频道中的视频中)(https://developers.google.com/youtube/v3/docs/search/list)将视频添加到播放列表(https://developers.google.com/youtube/v3/docs/playlistItems/insert)我能够成功地单独运行代码,但无法将它们组合在一起。具体而言:创建播放列表后,代码需要获取播放列表 ID。同样,当找到符合搜索条件的视频时,需要视频 ID 才能将其添加到播放列表中。我用过Python。拜托,任何人都可以帮忙吗?这是我的代码:import osimport google_auth_oauthlib.flowimport googleapiclient.discoveryimport googleapiclient.errorsscopes = ["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 = "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.search().list( part="snippet", forMine=True, maxResults=50, order="date", q="searchcriteria", type="video" ) response = request.execute() print(response) vid=response['videoId'] request2 = youtube.playlistItems().insert( part="snippet", body={ "snippet": { "playlistId": "myplaylistIdcomeshere", "resourceId": { "videoId": "vid", "kind": "youtube#video", "playlistId": "myplaylistIdcomeshereagain" }, "position": 0 } } ) respons2 = request2.execute() print(response2)if __name__ == "__main__": main()
2 回答
慕标5832272
TA贡献1966条经验 获得超4个赞
将项目插入播放列表的响应应返回包含播放列表 ID 的播放列表资源
https://developers.google.com/youtube/v3/docs/playlists#resource
同样,当您搜索视频时,您会收到一个search_resource
作为响应,其中包含带有ID的视频列表。
https://developers.google.com/youtube/v3/docs/search#resource
眼眸繁星
TA贡献1873条经验 获得超9个赞
只是想出了一个可行的方法。将这段代码放在请求之间可以做到这一点。在这里发帖,以防其他人遇到同样的问题:
for search_result in response.get('items', []): videos.append('%s (%s)' % (search_result['snippet']['title'], search_result['id']['videoId'])) vid=search_result['id']['videoId']
添加回答
举报
0/150
提交
取消