我试图把我在 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 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()
慕标5832272
眼眸繁星
相关分类