错误:“无法识别的参数:shell” - 适用于 Python 的 OAuth 2.0 Google API 客户端

Ksh*_*tij 2 python shell oauth-2.0 google-oauth google-api-python-client

我正在尝试进行纯粹的服务器端身份验证。我已遵循谷歌开发人员提供的代码示例中提到的所有过程。问题是execfile()在正常情况下工作正常python shell,但在python interactive shell.

以下是语句的日志。文件中还需要定义什么才能在Python Interactive Shellplaylistitems.py中执行。

代码playlistitems.py示例代码

Kshitij:trailers_backend Kshitij$ python manage.py shell
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
[GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.0.68)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> execfile("playlistitems.py")
usage: manage.py [--auth_host_name AUTH_HOST_NAME] [--noauth_local_webserver]
                 [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                 [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}]
manage.py: error: unrecognized arguments: shell
Kshitij:trailers_backend Kshitij$ 
Run Code Online (Sandbox Code Playgroud)

我通过更改本身的值来传递参数tools.py。以下是我收到的错误。我该如何解决?这个案例的端到端示例?

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/Kshitij/django project/trailers_backend/videoStore/getVideos.py", line 62, in getVideos
     credentials = run_flow(flow, storage, flags)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/argparse.py", line 1260, in add_argument
    if not args or len(args) == 1 and args[0][0] not in chars:
TypeError: 'bool' object has no attribute '__getitem__'
>>> 
Run Code Online (Sandbox Code Playgroud)

Ksh*_*tij 5

在@Stormie 的帮助下,我终于得到了工作代码。这是工作代码片段。

if credentials is None or credentials.invalid:
    flags = argparser.parse_args('--auth_host_name localhost --logging_level INFO'.split())
    credentials = run_flow(flow, storage, flags)
Run Code Online (Sandbox Code Playgroud)

parse_args()我现在才知道。需要ArgumentParser的parse_args()方法来传递参数。

如果您从命令行进行身份验证并且您的浏览器不支持 Javascript,您可能需要添加参数 --noauth_local_webserver:

flags = argparser.parse_args('--auth_host_name localhost --logging_level INFO --noauth_local_webserver'.split())
        credentials = run_flow(flow, storage, flags)
Run Code Online (Sandbox Code Playgroud)