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

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

问题描述

这可能需要一秒钟才能解释,所以请耐心等待:



我正在开发一个项目,需要我提供Google分析数据。我原先是在链接之后执行此操作的,因此在安装API之后客户端 pip install --upgrade google-api-python-client 并将其设置为 client_secrets.json ,想要安装gflags以执行 run()语句。 (即 credentials = run(FLOW,storage)

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


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

我最初使用gflags(几个月前),但它与我们的框架(金字塔)不兼容,所以我们将其删除,直到我们能够找出问题所在。为什么最好从gflags切换到 run_flow()是因为gflags 不推荐使用,所以我不想使用它像我一样。我现在要做的是切换到使用 run_flow()



这个问题是 run_flow()需要发送一个命令行参数给它,这不是一个命令行应用程序。我发现了一些有用的文档,但我坚持为 run_flow()函数创建标志。

在展示代码之前,还有一件事需要解释。



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



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





第二个链接非常有助于了解它将如何执行,所以现在当我尝试做类似的事情, sys.argv 会拉动我的虚拟环境的位置,这个环境运行又名 pserve 在我的 .ini 文件中(它存储我的机器的凭据以运行虚拟环境)。但是由于它期待着别的东西,所以会引发错误,而这正是我卡住的地方。




  • 我不知道需要通过 run_flow()
  • 我不知道为了语句 flags = parser.parse_args(argv [1:]),我需要传递哪些argv参数。 code>来检索正确的信息(我不知道正确的信息应该是什么)


代码:

  CLIENT_SECRETS = client_file.uri 
MISSING_CLIENT_SECRETS_MESSAGE ='%s丢失'%CLIENT_SECRETS
FLOW = flow_from_clientsecrets(
CLIENT_SECRETS,$ b $ 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()

如果凭证是None或credentials.invalid:
parser = arg parse.ArgumentParser(description = __ doc__,
formatter_class = argparse.RawDescriptionHelpFormatter,
parents = [tools.argparser])
flags = parser.parse_args(argv [1:])#我也可以(self,argv):
http://www.ibm.com/developerworks/cn/websphere/library/default.asp?url=&l=zh-cn&其中, httplib2.Http()
credentials = self.prepare_credentials(self,argv)
http = credentials.authorize(http)
return build('analytics','v3',http = http)

我调用一个传递 sys.argv 调用 initialize_service

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

尝试:
#查询和东西

我知道这是行不通的,因为我的应用程序不是命令行应用程序bu而是一个完整的综合服务,但我认为这值得一试。任何有关如何正确构建flags对象的想法?

解决方案

  from oauth2client import tools 

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

花了一些时间,但我爬出了两条陷阱,它让我陷入了困境:


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


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

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))

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

NotImplementedError: The gflags library must be installed to use tools.run(). Please install gflags or preferably switch to using tools.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()

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() 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.

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

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.

  • 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)

Code:

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)

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. have to use the argparser provided in tools
  2. I had to feed args an empty list to prevent it from reading args off the command line, which was a problem because I'm running it from inside a unittest (so different cmdline args).

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

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