2014-10-05 71 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不過,我必須指出,修改的核心軟件包不推薦,因爲您的下次更新軟件包時,更改將會被破壞。最乾淨的解決方案是更新到最新版本的所有內容,從而更輕鬆地完成您想要的任務。