python-3.x - 使用 YouTube 数据 API 时出现授权错误
问题描述
我正在尝试使用 YouTube Data API v3,但是当我运行我的脚本时出现以下错误:
这是我正在使用的脚本:
import os
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
from googleapiclient.http import MediaFileUpload
scopes = ["https://www.googleapis.com/auth/youtube"]
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_secret.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.thumbnails().set(
videoId="EoOUcFHsE9w",
# TODO: For this request to work, you must replace "YOUR_FILE"
# with a pointer to the actual file you are uploading.
media_body=MediaFileUpload("300coin.png")
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
当我运行这个脚本时,我在控制台中得到了这个:
Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=763961784735-ti8rm4futst2u81hdmnaf803e9gcfm82.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fyoutube&state=NQWjaXU1VCruBsePLHo0M8U6eDCsoF&prompt=consent&access_type=offline
Enter the authorization code:
当我点击链接时,会打开一个浏览器并显示以下通知:
Error 400: redirect_uri_mismatch
The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type. You can create a Client ID for native application at
在我用于授权 JavaScript 来源的 Web 应用程序的 ClientId 中:http://localhost:8080
. 对于授权的重定向 URI:http://localhost:8080
.
当我去域验证并想添加 localhost 时,我必须写http://www.localhost:8080
才能获得谷歌验证 .html 文件,但不用说,域http://www.localhost:8080
不起作用。那么我该如何解决这个问题呢?
我想要的只是运行一个脚本并在 Python 中更新我的缩略图。
解决方案
推荐阅读
- rx-java2 - 使用 Observable 和 take 运算符读取文件的 RxJava 问题
- javascript - 如何通过流星js中的模板数据传递变量?
- visual-studio - 向 VS 添加 WSDL 服务引用失败,因为生成的 xsd 文件包含 TFS 的非法字符
- python - 使用 asyncio 同时监听两个 websocket
- linux - HAProxy 配置端口问题
- c# - 错误 MSB3094:“DestinationFiles”指的是 1 个项目,“SourceFiles”指的是 428 个项目
- cmake - CMake if(DEFINED MY_COMPILE_DEF) 不起作用。如何使用预编译定义作为条件?
- python - 不能将 1 个数据帧附加到另一个?
- python - Django 表单提交按钮不起作用
- c - 从文件中读取数据块 (C)