【发布时间】:2022-07-28 13:45:59
【问题描述】:
我有一个视频列表,我想在 youtube api v3 的帮助下添加到播放列表中。我在我的代码以及开发人员控制台中设置了 oauth:
# Sample Python code for youtube.channels.list
# See instructions for running these code samples locally:
# https://developers.google.com/explorer-help/code-samples#python
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = [\"https://www.googleapis.com/auth/youtube\"]
# 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_secret_xxx.apps.googleusercontent.com.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)
然后我使用此功能将视频添加到我的播放列表中:
def add_video_to_playlist(youtube,videoID,playlistID):
add_video_request=youtube.playlistItems().insert(
part=\"snippet\",
body={
\'snippet\': {
\'playlistId\': playlistID,
\'resourceId\': {
\'kind\': \'youtube#video\',
\'videoId\': videoID
}
#\'position\': 0
}
}
).execute()
我的开发者帐户中的范围已设置: Screenshot
我收到的错误消息如下:
HttpError: <HttpError 403 when requesting https://youtube.googleapis.com/youtube/v3/playlistItems?part=snippet&alt=json returned \"Forbidden\". Details: \"[{\'message\': \'Forbidden\', \'domain\': \'youtube.playlistItem\', \'reason\': \'playlistItemsNotAccessible\'}]\">
我在哪里做错了什么?
-
documentation 中的
playlistItemsNotAccessible错误说:The request is not properly authorized to retrieve the specified playlist.= 播放列表是否存在? - 我没有在你的截图中看到googleapis.com/auth/youtube.force-ssl...检查this answer -
播放列表存在并且 force-ssl 在屏幕截图中。由于您的链接,仍然发现了问题,请参见下文。
标签: python youtube-api youtube-data-api google-developers-console