亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Youtube API v3:搜索視頻并將其上傳到播放列表

Youtube API v3:搜索視頻并將其上傳到播放列表

九州編程 2022-08-02 18:25:15
我試圖把我在 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


查看完整回答
反對 回復 2022-08-02
?
眼眸繁星

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']


查看完整回答
反對 回復 2022-08-02
  • 2 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號