【发布时间】:2015-05-02 19:33:39
【问题描述】:
我正在尝试创建一个简单的 python 脚本,允许我将 youtube 视频列表批量添加到我的播放列表中。
我遇到的问题是让这个脚本使用我的应用程序凭据通过 google api 进行身份验证。
我基本上只是使用https://developers.google.com/google-apps/calendar/quickstart/python 的示例身份验证脚本 还有这个 stackoverflow 问题 (Adding youtube video to playlist using Python)
主要的停止点是我不断收到错误:redirect_uri_mismatch。由于我是从笔记本电脑上的命令行调用脚本,所以错误是:The redirect URI in the request: http://localhost:8080/ did not match a registered redirect URI.
我已将 http://localhost:8080 设置为 JavaScript 来源,并将 http://localhost:8080/oauth2callback 设置为 重定向 URI
我正在使用以下内容(从 python shell 运行):
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client import tools
from oauth2client.tools import argparser, run_flow
import argparse, sys, os
flow = flow_from_clientsecrets('path to my CLIENT_SECRETS.json file', scope='https://www.googleapis.com/auth/youtube')
store = Storage('config/%s-oauth2.json' % sys.argv[0])
parser = argparse.ArgumentParser(parents=[tools.argparser])
flags = parser.parse_args()
credentials = run_flow(flow, store, flags)
然后终端打开我的浏览器,在浏览器中我收到 400 错误。以下内容被打印到终端:
您的浏览器已打开访问:
如果您的浏览器在另一台机器上,请退出并重新运行 带有命令行参数的应用程序 --noauth_local_webserver
我不确定我真正应该将什么作为重定向 URI 和 javascript 来源,但我不打算在我的笔记本电脑终端作为 python 脚本之外的任何地方运行它。文档提供了 https://example.com/ 和 https://example.com/oauth2callback 作为预填充值,但显然这不是我运行这个“应用程序”的地方,我知道这只是占位符的东西。
更新:不知道为什么,但我意识到应用程序发送给我的 url 将 redirect_uri 参数设置为 http://localhost:8080/,如果我将 oauth2callback 添加到 uri 而不是发送到它要求我接受 youtube 帐户管理的屏幕。所以这很奇怪。
更新 2:如果我通过开发者控制台将重定向 uri 更改为 http://localhost:8080/,我可以获得我的凭据,但我确信这不是最好的方法这个。
【问题讨论】:
标签: python google-api-python-client