gpt4 book ai didi

python - 在 python 中没有本地浏览器的 Google Analytics API 访问

转载 作者:太空狗 更新时间:2023-10-29 18:31:40 33 4
gpt4 key购买 nike

我想使用 Python 查询 Google Analytics API 以定期从我的 Analytics 帐户下载数据并将数据存储在本地数据库中。我基本上按照 basic tutorial 中给出的步骤进行操作.我正在使用 Google client API library for Python在这个过程中。

到目前为止,当我在本地开发机器 (Mac) 上运行脚本时,它运行良好。当我启动脚本时,我的浏览器会打开,系统会提示我授予从应用程序访问我的 Analytics 数据的权限。之后,我可以随意运行我的脚本并访问我的数据。

在我的服务器(Ubuntu,只有终端可用)上,w3m 浏览器打开,但我无法从那里访问我的 Google 帐户。我只能退出 w3m 并使用 Ctrl-C 终止程序。出现如下错误消息:

Your browser has been opened to visit:

https://accounts.google.com/o/oauth2/auth?scope=some_long_url&access_type=offline

If your browser is on a different machine then exit and re-run this application with the command-line parameter

--noauth_local_webserver

但是,当我使用参数 --noauth_local_webserver 运行我的脚本时,我得到了相同的结果 - w3m 打开并且我无法进行身份验证。

如何让 --noauth_local_webserver 工作?我有另一种方法可以在同一台机器上没有本地浏览器的情况下进行身份验证吗?

最佳答案

当您使用 FLAGS = gflags.FLAGS 时,您实际上需要将命令行参数传递给 FLAGS(这可能也可能不会让我感到困惑:))。参见 here有关如何执行此操作的以分析为中心的示例(下面的代码,因为链接往往会在一段时间后消失)。一般的想法是将 argv 参数传递给 FLAGS 变量,然后其他模块可以使用它。

# From samples/analytics/sample_utils.py in the google-api-python-client source

def process_flags(argv):
"""Uses the command-line flags to set the logging level.

Args:
argv: List of command line arguments passed to the python script.
"""

# Let the gflags module process the command-line arguments.
try:
argv = FLAGS(argv)
except gflags.FlagsError, e:
print '%s\nUsage: %s ARGS\n%s' % (e, argv[0], FLAGS)
sys.exit(1)

# Set the logging according to the command-line flag.
logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))

此外,事实证明我们并不孤单!您可以追踪this bug查看何时将其添加到文档中。

关于python - 在 python 中没有本地浏览器的 Google Analytics API 访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12956086/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com