我試圖把我在 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
提交
取消