如何在没有 gflags 的情况下获取 Google Analytics 凭据 - 使用 run_flow() 代替? [英] How to get Google Analytics credentials without gflags - using run_flow() instead?

查看:19
本文介绍了如何在没有 gflags 的情况下获取 Google Analytics 凭据 - 使用 run_flow() 代替?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能需要一点时间来解释,所以请耐心等待:

This may take a second to explain so please bear with me:

我正在处理一个需要我提取谷歌分析数据的工作项目.我最初是按照这个 link 这样做的,所以在安装 API 之后客户端 pip install --upgrade google-api-python-client 并设置类似 client_secrets.json 的东西,它希望安装 gflags 以执行 run() 语句.(即 credentials = run(FLOW, storage))

I'm working on a project for work that requires me to pull in google analytics data. I originally did this following this link, so after installing the API client pip install --upgrade google-api-python-client and setting things up like the client_secrets.json, it wanted gflags to be installed in order to execute the run() statement. (i.e credentials = run(FLOW, storage))

现在,我收到了安装 gflags 或更好地使用 run_flow() 的错误消息(确切的错误消息是这样的):

Now, I was getting the error message to install gflags or better to use run_flow() (exact error message was this):

NotImplementedError:必须安装 gflags 库才能使用 tools.run().请安装 gflags 或最好切换到使用 tools.run_flow().

NotImplementedError: The gflags library must be installed to use tools.run(). Please install gflags or preferably switch to using tools.run_flow().

我最初使用 gflags(几个月前),但它与我们的框架(金字塔)不兼容,因此我们将其删除,直到我们弄清楚问题所在.最好从 gflags 切换到 run_flow() 的原因是 gflags 已经弃用,所以我不想像以前那样使用它.我现在要做的是切换到使用 run_flow()

I originally used gflags (a few months ago), but it wasn't compatible with our framework (pyramid), so we removed it until we could figure out what the issue was. And the reason why it's preferable to switch from gflags to run_flow() is because gflags has been deprecated, so I don't want to use it like I had. What I'm trying to do now is switch over to using run_flow()

这个问题是 run_flow() 需要向它发送命令行参数,这不是命令行应用程序.我找到了一些有用的文档,但我一直在为 run_flow() 函数构建标志.

The issue with this is run_flow() expects a command line argument to be sent to it and this is not a command line application. I found some documentation that was helpful but I'm stuck on building the flags for the run_flow() function.

在展示代码之前还要解释一件事.

Before showing code one more thing to explain.

run_flow() 接受三个参数 (此处提供文档).它像 run() 一样接受流和存储,但它也接受一个标志对象.gflags 库构建了一个在 oauth2client 执行方法中使用的标志 ArgumentParser 对象.

run_flow() takes three arguments (documentation here). It takes the flow and storage just like run() does, but it also takes a flags object. the gflags library built a flags ArgumentParser object that was used in the oauth2client execution method.

一些有助于构建 argumentParser 对象的其他链接:

a few other links that were helpful in building the argumentParser object:

第二个链接对于了解它将如何执行非常有帮助,所以现在当我尝试做类似的事情时,sys.argv 会拉入我正在运行的虚拟环境的位置,也就是 pserve 并拉入我的 .ini 文件(它存储我的机器运行虚拟环境的凭据).但这会引发错误,因为它期待其他东西,这就是我被卡住的地方.

The second link is very helpful to see how it would be executed, so now when I try to do something similar, sys.argv pulls in the location of my virtual environment that is running aka pserve and also pulls in my .ini file (which stores credentials for my machine to run the virtual environment). But that throws an error because its expecting something else, and this is where I'm stuck.

  • 我不知道我需要构建什么标志对象才能通过 run_flow()
  • 我不知道我需要传递哪些 argv 参数才能使语句 flags = parser.parse_args(argv[1:]) 检索正确的信息(我不知道什么是正确的信息应该是)
  • I don't know what flags object I need to build to send through run_flow()
  • I don't know what argv arguments I need passed in order for the statement flags = parser.parse_args(argv[1:]) to retrieve the correct information (I don't know what the correct information is supposed to be)

代码:

CLIENT_SECRETS = client_file.uri
MISSING_CLIENT_SECRETS_MESSAGE = '%s is missing' % CLIENT_SECRETS
FLOW = flow_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/analytics.readonly',
    message=MISSING_CLIENT_SECRETS_MESSAGE
)
TOKEN_FILE_NAME = 'analytics.dat'

def prepare_credentials(self, argv):
    storage = Storage(self.TOKEN_FILE_NAME)
    credentials = storage.get()

    if credentials is None or credentials.invalid:
        parser = argparse.ArgumentParser(description=__doc__,
            formatter_class=argparse.RawDescriptionHelpFormatter,
            parents=[tools.argparser])
        flags = parser.parse_args(argv[1:]) # i could also do just argv, both error
        credentials = run_flow(self.FLOW, storage, flags) 
    return credentials

def initialize_service(self, argv):
    http = httplib2.Http()
    credentials = self.prepare_credentials(self, argv)
    http = credentials.authorize(http)
    return build('analytics', 'v3', http=http)

我调用了一个传递 sys.argv 的主函数,该函数调用了 initialize_service

I call a main function passing sys.argv that calls the initialize_service

def main(self, argv):
    service = self.initialize_service(self, argv)

    try:
        #do a query and stuff here

我知道这行不通,因为我的应用程序不是命令行应用程序,而是一个完整的集成服务,但我认为值得一试.关于如何正确构建标志对象的任何想法?

I knew this wouldn't work because my application is not a command line application but rather a full integrated service but I figured it was worth a shot. Any thoughts on how to build the flags object correctly?

推荐答案

from oauth2client import tools

flags = tools.argparser.parse_args(args=[])
credentials = tools.run_flow(flow, storage, flags)

费了一番功夫,但还是爬出了它让我掉进的两个陷阱:

Took a bit of mucking about but climbed my way out the two traps it dropped me into:

  1. 必须使用工具中提供的 argparser
  2. 我不得不为 args 提供一个空列表,以防止它从命令行读取 args,这是一个问题,因为我是从单元测试内部运行它的(因此不同的 cmdline args).

这篇关于如何在没有 gflags 的情况下获取 Google Analytics 凭据 - 使用 run_flow() 代替?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆