更改noauth_local_webserver的默认状态?

时间:2014-10-05 14:32:45

标签: python oauth-2.0 google-api youtube-api google-oauth

我目前正在为我的社区制作一个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

提前致谢,Amar!

1 个答案:

答案 0 :(得分:7)

您错过了一些代码。更新到最新的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但是,我必须指出修改核心包是不建议使用,因为下次更新软件包时,您的更改将被破坏。最干净的解决方案是更新到所有内容的最新版本,这样可以更轻松地完成您想要的操作。