我目前正在为我的社区制作一个GUI YouTube视频上传器,但由于我不希望所有用户都能获得我的client_id和client_secret,所以我对它们进行了编码。问题是,每当程序运行时(它不是使用参数从命令行运行的,而是从Tkinter GUI获得这些信息),它就会开始通过web链接对用户进行身份验证,该链接包含真实的client_id和client_secret。我尝试使用--noauth_local_webserver参数,但没有成功,因为没有从命令行运行任何东西(我还没有找到在没有命令行的情况下运行此参数的方法)。正如我在官方文档中看到的,这个参数默认设置为"False",有什么方法可以更改它吗,或者有什么方法禁用web身份验证吗?这是我用来验证和开始上传视频的代码(这几乎是官方文档中的默认代码,几乎没有什么变化,所以它符合我的需求):
def get_authenticated_service():
makeitreal() #this is function which decodes encoded client_id and client_secret
flow = flow_from_clientsecrets(os.path.abspath(os.path.join(os.path.dirname(__file__), "client_secrets.json")), scope=YOUTUBE_UPLOAD_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run(flow, storage)
return build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION,
http=credentials.authorize(httplib2.Http()))
def initialize_upload():
makeitreal() #this is function which decodes encoded client_id and client_secret
youtube = get_authenticated_service()
os.remove(os.path.join(os.path.dirname(__file__), "upload_video.py-oauth2.json")) #I use this to remove this json since it's not being used anymore and it contains client_id and client_secret
tags = None
insert_request = youtube.videos().insert(
part="snippet,status",
body=dict(
snippet=dict(
title=video_title, #####
description=video_desc, # These 3 parameters are not being gathered through command line as it was in default code, I changed it so it gets these from Tkinter GUI
tags=video_keywords, ####
categoryId="22"
),
status=dict(
privacyStatus=VALID_PRIVACY_STATUSES[0]
)
),
# chunksize=-1 means that the entire file will be uploaded in a single
# HTTP request. (If the upload fails, it will still be retried where it
# left off.) This is usually a best practice, but if you're using Python
# older than 2.6 or if you're running on App Engine, you should set the
# chunksize to something like 1024 * 1024 (1 megabyte).
media_body=MediaFileUpload(filename, chunksize=-1, resumable=True)
)
makeitfake() #this is function which encodes previously decoded client_id and client_secret
resumable_upload(insert_request) #this function uploads video
提前谢谢,阿玛!
您缺少一些代码。更新到最新的API和示例,它很简单:args.noauth_local_webserver = True
无论如何,如果您想尝试自己添加对argparser的支持,这里有一些代码。不再有跑步,而是跑步流。但是您可以将args作为第三个参数传递给现有的run函数。
from oauth2client.tools import argparser, run_flow
args = argparser.parse_args()
args.noauth_local_webserver = True
credentials = run_flow(flow, storage, args)
或者,如果你必须让它工作,你可以修改oauth2client/tools.py并搜索if not flags.noauth_local_webserver
,就在上面添加flags.noauth_local_webserver = True
。但是,我必须指出,不建议修改核心包,因为下次更新包时,你的更改会被破坏。最干净的解决方案是更新到所有内容的最新版本,这样可以更容易地做您想做的事情。