无需使用python的本地浏览器即可访问Google Analytics(分析)API [英] Google Analytics API access without local browser in python
问题描述
我想使用Python查询Google Analytics(分析)API,以定期从我的Google Analytics(分析)帐户下载数据并将数据存储在本地数据库中.我基本上是按照基本教程中给出的步骤进行操作的.在此过程中,我正在使用适用于Python的Google客户端API库.
I want to query the Google Analytics API using Python to periodically download data from my Analytics account and store data in a local database. I am basically following the steps as given in the basic tutorial. I am using the Google client API library for Python in this process.
到目前为止,当我在本地开发机(Mac)上运行脚本时,脚本运行良好.启动脚本时,浏览器将打开,并提示我授予从该应用访问我的Google Analytics(分析)数据的权限.之后,我可以根据需要多次运行脚本并访问数据.
My script is working fine so far when I am running it on my local dev machine (Mac). When I start the script, my browser opens and I am prompted to grant access to my Analytics data from the app. Afterwards I can run my script as often as I want and get access to my data.
在我的服务器(Ubuntu,只有可用的终端)上,打开了w3m
浏览器,但是我无法从那里访问我的Google帐户.我只能退出w3m
并使用Ctrl-C
杀死程序.出现错误消息,例如:
On my server (Ubuntu, only terminal available), the w3m
browser opens, but I cannot access my Google account from there. I can only quit w3m
and kill the program with Ctrl-C
. There is an error message like:
您的浏览器已打开以访问:
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
但是,当我使用参数--noauth_local_webserver
运行脚本时,会得到相同的结果-w3m
打开并且无法进行身份验证.
However when I run my script with the parameter --noauth_local_webserver
, I get the same results - w3m
opens and I cannot authenticate.
如何使--noauth_local_webserver
正常工作?在同一台机器上没有本地浏览器的情况下,我还有另一种身份验证方法吗?
How can I get the --noauth_local_webserver
to work? I there another way to authenticate without a local browser on the same machine?
推荐答案
使用FLAGS = gflags.FLAGS
时,实际上需要将命令行参数传递给FLAGS
(这可能也可能不会使我绊倒:)).请参阅此处以获取Analytics-此操作的中心示例(下面的代码,因为链接可能会在一段时间后消失).一般的想法是将argv
参数传递到FLAGS
变量中,然后该变量可用于其他模块.
When you use FLAGS = gflags.FLAGS
, you actually need to pass the command-line arguments to FLAGS
(this may or may not have tripped me up as well :) ). See here for an Analytics-centric example of how to do it (code below as links tend to go away after a while). General idea is that argv
arguments are passed into the FLAGS
variable, which then become available to other modules.
# 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))
而且,事实证明我们并不孤单!您可以跟踪此错误,以查看何时添加此错误.文档.
Also, turns out that we aren't alone! You can track this bug to see when this will get added the documentation.
这篇关于无需使用python的本地浏览器即可访问Google Analytics(分析)API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!