首页 文章

在Google Apps Engine python项目中使用Google Drive API和服务帐户,并进行本地调试

提问于
浏览
7

在过去的几天里(在我可以花在这个项目上的奇数小时),我一直在努力开始在GAE python中使用带有服务帐户的Google Drive API进行本地调试 .

我的设置:

  • Eclipse 4.4

  • 适用于Python 1.8.0的Google App Engine SDK

  • Google API客户端Python GAE 1.1

我在app.yaml中激活(以及其他)这些第三方库:

- name: pycrypto
  version: latest
- name: ssl
  version: latest

这是我对设置软件的理解,在几个陈述中:

  • 因为我的应用没有't need to access user files, but a file which is specific to the app, the app should use a '服务帐户'拥有并访问Google Cloud 端硬盘上的文件

  • 服务帐户可以通过两种方式进行身份验证:(1)通过API密钥和(2)通过私钥凭据进行身份验证

  • 使用SDK开发GAE应用程序时,需要考虑两个环境:本地系统(用于调试)和GAE服务器(用于部署)

  • API密钥认证没有't work (and will never work) when running on the local system, because of the two-legged authentication (don' t完全掌握这个...,但似乎是真的)

我真的想要本地调试工具,因为我正在学习python并学习google驱动器接口,因此在服务器上进行调试是一个很大的负担 .

所以我需要获取在本地系统上工作的私钥凭证 . 但后来我遇到了问题“ImportError:无法导入名称SignedJwtAssertionCredentials” . 几乎尝试了我在网上找到的所有东西:

  • 使用python 2.7运行时并启用pycrypto库

  • 将google-api-python-client-gae升级到1.1(包含此修复)

  • 在我的系统上安装了OpenSSL(但可能没有成功设置正确的路径)

  • 阅读本地安装pycrypto的说明,但认为它们已经过时了

=>我要解释的第一个问题是:是否可以在GAE SDK中使用Python在本地系统上对Google Drive API进行身份验证?也许答案很简单'不'?

=>如果答案是'是',那么是否会有一个示例设置和代码示例,以显示实现此本地身份验证的方式?

=>错误日志(下面)似乎表明pycrypto仍然存在问题,但是文档明确地说它包含在Python 2.7 GAE运行时环境中 .

=>也许(请确认)我对本地和服务器python设置之间的区别感到困惑 . 当我在Eclipse中查看'Run Local'PYTHONPATH时,它包括(1)我的项目文件夹,(2)google-api-client-python-gae文件夹(似乎不包括pycrypto !!),而GAE运行时确实 - 有什么区别? (3)我的本地Python 2.7部署 . 那么在本地配置中我需要模仿服务器配置才能开始调试?

这是我使用私钥凭据进行身份验证的代码:

from oauth2client.client import SignedJwtAssertionCredentials
f = file(SERVICE_ACCOUNT_PKCS12_FILE_PATH, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key, scope=OAUTH_SCOPE)
http = httplib2.Http()
http = credentials.authorize(http)
return build('drive', 'v2', http=http)

这是我的错误日志:

ERROR    2013-06-18 00:59:57,562 dev_appserver_import_hook.py:1251] Third party package Crypto was enabled in app.yaml but not found on import. You may have to download and install it.
ERROR    2013-06-18 00:59:59,255 dev_appserver_import_hook.py:1251] Third party package Crypto was enabled in app.yaml but not found on import. You may have to download and install it.
ERROR    2013-06-18 00:59:59,289 webapp2.py:1552] import_string() failed for 'illustrations.SyncHandler'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Original exception:

ImportError: cannot import name SignedJwtAssertionCredentials

Debugged import:

- 'illustrations' not found.
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1272, in default_dispatcher
    self.handlers[handler] = handler = import_string(handler)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1850, in import_string
    return getattr(__import__(module, None, None, [obj]), obj)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1766, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1630, in FindAndLoadModule
    description)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1577, in LoadModuleRestricted
    description)
  File "C:\Users\vic\Dropbox\Development\Eclipse-juno-workspace\Missale\src\illustrations.py", line 6, in <module>
    import drive
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1766, in load_module
    return self.FindAndLoadModule(submodule, fullname, search_path)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1630, in FindAndLoadModule
    description)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 692, in Decorate
    return func(self, *args, **kwargs)
  File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\dev_appserver_import_hook.py", line 1577, in LoadModuleRestricted
    description)
  File "C:\Users\vic\Dropbox\Development\Eclipse-juno-workspace\Missale\src\drive.py", line 6, in <module>
    from oauth2client.client import SignedJwtAssertionCredentials
ImportStringError: import_string() failed for 'illustrations.SyncHandler'. Possible reasons are:

- missing __init__.py in a package;
- package or module path not included in sys.path;
- duplicated package or module name taking precedence in sys.path;
- missing module, class, function or variable;

Original exception:

ImportError: cannot import name SignedJwtAssertionCredentials

Debugged import:

- 'illustrations' not found.

[更新]回顾我的问题,我想'll need to to have a closer look in to installing pycrypto locally. If this is the fix, I'我将在this article上发表反馈,请求添加关于GAE服务器运行时库和本地SDK库之间差异的说明 . 我也会在这里添加安装说明 .

[update2] SignedJwtAssertionCredentials导入问题已经消失,但是tlslite包上出现了另一个导入问题 . 我不知道如何解决这个问题,因为导入看起来很完美,我从头开始重新配置整个IDE . 我现在安装了another precompiled pycrypto library并按照错误消息中的提示进行操作,并将我的.p12私钥文件转换为.pem文件 . 请注意,openssl创建的.pem文件在"-----BEGIN"之前包含4个文本行,我必须手动删除,以便oauth2client识别.pem文件!

[update3]从头开始重新配置IDE时,我忽略了使用'old_dev_appserver.py'在本地运行应用程序而不是'dev_appserver.py' . The latter will not enable breakpoints!但它看起来与SignedJwtAssertionCredentials导入问题有关 . 使用'dev_appserver.py',我没有导入问题(但没有断点),并且使用'old_dev_appserver.py',我可以重现导入问题 . 所以'old_dev_appserver.py'可能一直是问题的一部分!

1 回答

  • 1

    是的 - 您可以使用SignedJwtAssertionCredentials连接到Google服务 . 我编写的代码一次性完成,效果很好 . 使用该类是一个问题 . 在GAE上提供了像你写的PyCrypto库,但是在本地你必须手动安装它 . GAE有他自己更改了这个库的版本,所以他们没有提供她的源代码 . 该库的第二个问题是她是为使用她的指定机器编译的 . 如果查看该类的源代码,您将看到她需要一些依赖项才能工作 . 如果他们不符合该课程的要求,则无法从外部代码访问 .

    关于在旧的和新的dev_server中导入的问题我不能写任何东西,因为我对此一无所知 .

相关问题