我一直在尝试制作一个脚本,用视频观看次数更新 youtube 标题,例如 Tom Scott 视频。但是我在身份验证时不断收到此错误。
我得到了验证它的代码,但我只是说我没有足够的权限。
<HttpError 400 when requesting https://youtube.googleapis.com/youtube/v3/videos?part=%28%27snippet%2Cstatus%2Clocalizations%27%2C%29&alt=json returned "'('snippet'". Details: "[{'message': "'('snippet'", 'domain': 'youtube.part', 'reason': 'unknownPart', 'location': 'part', 'locationType': 'parameter'}]"> File "C:\Users\erik\Documents\PYTHON CODE\view_changer\example.py",
line 49, in main response_update = request_update.execute()
这是我的代码 如果问题不在代码中,我可能对它可能是什么有其他想法。当我第一次设置 oauth 时,我不知道我需要选择用户将授予的权限(我知道新手错误(:)但是因为我在转到身份验证链接后请求许可时更新了它代码返回给我,它不会为我告诉它做的所有事情征得许可。
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
scopes = ["https://www.googleapis.com/auth/youtube"]
api_key = "..."
video_1_part_view = 'statistics'
video_1_id_view = 'hVuoPgZJ3Yw'
video_1_part_update= "snippet,status,localizations",
video_1_part_body_update= { "id": "1y94SadSsMU",
"localizations": {"es": { "title": "no hay nada a ver aqui",
"description": "Esta descripcion es en español."
}
}
}
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 = (r"C:\Users\erik\Documents\PYTHON CODE\view_changer\client_secret_379587650859-bbl63je7sh6kva19l33auonkledt09a9.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)
#check views
request_view= youtube.videos().list( part = video_1_part_view, id = video_1_id_view )
respons_view = request_view.execute()
views = respons_view['items'][0]['statistics']['viewCount']
print (views)
#update video
request_update = youtube.videos().update(part= video_1_part_update, body = video_1_part_body_update)
response_update = request_update.execute()
print(response_update)
main()
# if __name__ == "__main__":
# main()
感谢关注
答案 0 :(得分:1)
根据官方文档,part
API端点的请求参数part
定义为:
https://www.jetbrains.com/help/rider/Navigation_and_Search_Structural_Navigation.html(字符串)
part
参数指定 API 响应将包含的一个或多个 video
资源属性的逗号分隔列表。
[...]
但是您将该请求参数设置为:
('snippet,status,localizations',)
。
那是因为(尽管上面的代码没有显示)您将变量 video_1_part_view
定义为:
video_1_part_view = "snippet,status,localizations",
(请注意结尾的逗号)。这使得 video_1_part_view
成为 Python tuple
,您不需要将其发送到 API 端点。
变量 video_1_part_update
也是如此。
只需删除尾随的逗号,一切都会好起来的。