python - 更改 noauth_local_webserver 的默认状态?

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

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

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 作为第三个参数传递给现有的运行函数。

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 但是,我必须指出,不建议修改核心包,因为您的更改将在您下次更新包时被破坏。最简洁的解决方案是将所有内容更新到最新版本,这样可以更轻松地做您想做的事。

关于python - 更改 noauth_local_webserver 的默认状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26203571/

相关文章:

python -/lib64/libc.so.6 : version `GLIBC_2.14' not found - error from the application build using pyinstaller

java - 如何从linkedin认证中oauth的回调URL中获取code和state

python - Google OAuth2 命令行示例

ruby - 不要使用 google api ruby​​ 客户端请求离线访问

google-api - 是否可以在不代表域范围委派中的用户的情况下从服务帐户调用 API?

python - diy Tornado openshift 套接字错误

python - 使用预先确定的文本启动 raw_input()

python - 文档中令人困惑的 'readline' 警告

android - AccountManagerFuture.getResults 抛出 IOException,而连接处于 Activity 状态

delphi - 如何在没有用户交互的情况下验证 Google Calendar API v3?