Python Youtube API v3:搜索视频并将其上载到播放列表

Python Youtube API v3:搜索视频并将其上载到播放列表,python,youtube-data-api,Python,Youtube Data Api,我试图将我在中发现的三个单独代码组合在一起作为示例: 在我的频道()中创建播放列表 搜索标题中包含给定关键字的视频(在频道中的视频中)() 将视频添加到播放列表() 我能够成功地单独运行代码,但无法将它们合并。具体来说:创建播放列表后,代码需要获取播放列表ID。同样,当找到符合搜索条件的视频时,需要视频ID才能将其添加到播放列表中。 我用过Python。拜托,有人能帮忙吗 这是我的代码: import os import google_auth_oauthlib.flow import g

我试图将我在中发现的三个单独代码组合在一起作为示例:

  • 在我的频道()中创建播放列表

  • 搜索标题中包含给定关键字的视频(在频道中的视频中)()

  • 将视频添加到播放列表()

我能够成功地单独运行代码,但无法将它们合并。具体来说:创建播放列表后,代码需要获取播放列表ID。同样,当找到符合搜索条件的视频时,需要视频ID才能将其添加到播放列表中。 我用过Python。拜托,有人能帮忙吗

这是我的代码:

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 = "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()
从搜索部分的响应中,我有以下内容(编辑以删除不必要的详细信息):

{'kind':'youtube#searchListResponse','etag':'longetag','nextPageToken':'anotherlongtext=','pageInfo':{'totalResults':1,'resultsPerPage':50},'items':[{'kind':'youtube#searchResult','etag':','TheTagofVideodFind','id':{'kind':'youtube#视频,'videoId':'This'IsWhatIwant','snippet'{'publishedAt':'2020-02-22T19:33:03.000Z','channelId':'MyChannelTitle','title':'titleof video','description':'','thumbnails':{VideoThumbnails}},'channelTitle':'MyChannelTitle','liveBroadcastContent':'none'}}}

我试过了 vid=响应['videoId'] vid=响应['id']


但是它们都不起作用

将项目插入播放列表的响应应该返回一个包含播放列表ID的

类似地,当您搜索视频时,会得到一个包含带有ID的视频列表


刚刚想出了一个可行的方法。将这段代码放在两个请求之间就可以了。在此处发布,以防其他人遇到相同的问题:

for search_result in response.get('items', []):
      videos.append('%s (%s)' % (search_result['snippet']['title'],
                                 search_result['id']['videoId']))
      vid=search_result['id']['videoId']  

还可以包括您使用的代码段吗谢谢,我已经添加了代码和我尝试过的选项。我阅读了文档,但不知道如何从响应中检索“id”