Cannot get authentication with google api from python script to work - Error: redirect_uri_mismatch
Cannot get authentication with google api from python script to work - Error: redirect_uri_mismatch
我正在尝试创建一个简单的 python 脚本,允许我将 YouTube 视频列表批量添加到我的播放列表中。
我遇到的问题是让此脚本使用我的应用凭据向 google api 进行身份验证。
我基本上只是在 https://developers.google.com/google-apps/calendar/quickstart/python 使用示例身份验证脚本
和这个 Whosebug 问题 (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 错误。以下内容打印到终端:
Your browser has been opened to visit:
If your browser is on a different machine then exit and re-run this
application with the command-line parameter
--noauth_local_webserver
我不确定我真正应该将什么作为重定向 URI 和 javascript 来源,但我不打算 运行 除了作为 python 来自我笔记本电脑终端的脚本。该文档提供 https://example.com/
和 https://example.com/oauth2callback
作为预填充值,但显然这不是我 运行 宁此 "app" 的地方,我知道那只是占位符的东西。
UPDATE:不知道为什么,但我意识到 url 应用程序发送给我的 redirect_uri 参数设置为 http://localhost:8080/
,如果我将 oauth2callback 添加到 uri,我将被发送到要求我接受 youtube 帐户管理的屏幕。所以这很奇怪。
更新 2:如果我通过开发人员控制台将我的重定向 uris 更改为 http://localhost:8080/
,我可以获得我的凭据,但我确信这不是解决这个问题的最好方法。
如果您只是 运行 通过您的终端使用本机主机,您可以通过创建一个客户端 ID 作为已安装的应用程序,然后选择其他来使用本机主机。如果您尝试使用 Web 应用程序客户端 ID 执行此操作并且只希望它是本地的,那么您可以使用这些:
JavaScript 来源:http://localhost:your_port_here/
重定向 URI:http://localhost:your_port_here/url_path
其中 url_path 是您希望 google 在身份验证后发送给您的位置。
编辑:教程代码仅在您使用本机应用程序时有效。如果您确实打算创建网络应用程序,请参阅单独的说明。
redirect_uri
参数设置为 http://localhost:8080/ 是设计使然。当您调用 tools.run_flow()
时,它会在幕后启动自己的服务器并启动 oauth 流程。然后它期望 redirect_uri
重定向回它启动的服务器(它正在侦听端口 8080),以便它可以使用它收到的 access_token
创建凭据并将它们存储起来。
我正在尝试创建一个简单的 python 脚本,允许我将 YouTube 视频列表批量添加到我的播放列表中。
我遇到的问题是让此脚本使用我的应用凭据向 google api 进行身份验证。
我基本上只是在 https://developers.google.com/google-apps/calendar/quickstart/python 使用示例身份验证脚本 和这个 Whosebug 问题 (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 错误。以下内容打印到终端:
Your browser has been opened to visit:
If your browser is on a different machine then exit and re-run this application with the command-line parameter --noauth_local_webserver
我不确定我真正应该将什么作为重定向 URI 和 javascript 来源,但我不打算 运行 除了作为 python 来自我笔记本电脑终端的脚本。该文档提供 https://example.com/
和 https://example.com/oauth2callback
作为预填充值,但显然这不是我 运行 宁此 "app" 的地方,我知道那只是占位符的东西。
UPDATE:不知道为什么,但我意识到 url 应用程序发送给我的 redirect_uri 参数设置为 http://localhost:8080/
,如果我将 oauth2callback 添加到 uri,我将被发送到要求我接受 youtube 帐户管理的屏幕。所以这很奇怪。
更新 2:如果我通过开发人员控制台将我的重定向 uris 更改为 http://localhost:8080/
,我可以获得我的凭据,但我确信这不是解决这个问题的最好方法。
如果您只是 运行 通过您的终端使用本机主机,您可以通过创建一个客户端 ID 作为已安装的应用程序,然后选择其他来使用本机主机。如果您尝试使用 Web 应用程序客户端 ID 执行此操作并且只希望它是本地的,那么您可以使用这些:
JavaScript 来源:http://localhost:your_port_here/
重定向 URI:http://localhost:your_port_here/url_path 其中 url_path 是您希望 google 在身份验证后发送给您的位置。
编辑:教程代码仅在您使用本机应用程序时有效。如果您确实打算创建网络应用程序,请参阅单独的说明。
redirect_uri
参数设置为 http://localhost:8080/ 是设计使然。当您调用 tools.run_flow()
时,它会在幕后启动自己的服务器并启动 oauth 流程。然后它期望 redirect_uri
重定向回它启动的服务器(它正在侦听端口 8080),以便它可以使用它收到的 access_token
创建凭据并将它们存储起来。