2014-10-05 67 views
3

我目前正在为我的社区制作GUI YouTube视频上传器,但由于我不希望所有用户都获得我的client_id和client_secret,所以我对它们进行了编码。问题是,无论程序何时运行(它不是从命令行使用参数运行,它从Tkinter GUI获取这些信息),它开始通过包含真正的client_id和client_secret的web链接对用户进行身份验证。我尝试使用--noauth_local_webserver参数,但没有成功,因为没有从命令行运行(我没有找到方法来运行此参数没有命令行)。正如我在官方文档中看到的那样,默认情况下,此参数设置为“False”,是否有任何方法可以更改它,或者有什么方法可以禁用Web身份验证?这是我的代码,我用它来进行身份验证,并开始上传视频(这几乎是默认的官方文档,有一些变化,以便它适合我的需要):提前,阿马尔更改noauth_local_webserver的默认状态?

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 

谢谢!

回答

4

您错过了一些代码。更新到最新的API和示例,它很简单:args.noauth_local_webserver = True

无论如何,这里的一些代码如果你想尝试自己添加对argparser的支持。不再有运行,而是run_flow。但是您可以将args作为第三个参数传递给您的现有运行功能。

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不过,我必须指出,修改的核心软件包不推荐,因为您的下次更新软件包时,更改将会被破坏。最干净的解决方案是更新到最新版本的所有内容,从而更轻松地完成您想要的任务。