我正在将一个非常简单的应用程序从Google App Engine迁移到DigitalOcean。
所有应用程序都会:处理AJAX请求,查询我的BigQuery数据集,并将响应作为JSON返回。
我在virtualenv中运行Python 2.7,最近的Django。
我已经安装了Google Python客户端API模块,OAuth2Client模块以及其他一些模块。
对于我的生活,我无法导入必要的模块以成功连接到BigQuery,因此我在GAE上运行良好的应用程序在DigitalOcean上失败。
# this works fine
import oauth2client
但是......
# this throws "global name 'appengine' is not defined"
credentials = appengine.AppAssertionCredentials(scope=_SCOPE)
我尝试过导入appengine:
from oauth2client import appengine
但那只是抛出
No module named appengine.api
我在virtualenv中检查了我的lib / dir,OAuth2Client就在那里。
这是我的virtualenv中的site-packages目录:
(app_name)root@app_name:/webapps/app_name/lib/python2.7/site-packages# l
total 1104
drwxr-xr-x 2 root root 4096 May 3 00:00 apiclient
drwxr-xr-x 2 root root 4096 May 3 00:00 bigquery-2.0.17-py2.7.egg-info
-rw-r--r-- 1 root root 81577 May 3 00:00 bigquery_client.py
-rw-r--r-- 1 root root 86973 May 3 00:00 bigquery_client.pyc
-rw-r--r-- 1 root root 78673 May 3 00:00 bq.py
-rw-r--r-- 1 root root 76044 May 3 00:00 bq.pyc
drwxr-xr-x 3 root root 4096 May 3 00:00 dateutil
drwxr-xr-x 2 root root 4096 May 3 00:00 discovery
drwxrwxr-x 17 app_name users 4096 May 2 12:11 django
drwxrwxr-x 2 app_name users 4096 May 2 12:11 Django-1.6.4.dist-info
-rw-r--r-- 1 root root 239 May 2 23:20 easy-install.pth
-rw-rw-r-- 1 app_name users 126 May 2 12:10 easy_install.py
-rw-rw-r-- 1 app_name users 318 May 2 12:10 easy_install.pyc
-rw-r--r-- 1 root root 104236 May 3 00:00 gflags.py
-rw-r--r-- 1 root root 97904 May 3 00:00 gflags.pyc
-rw-r--r-- 1 root root 6977 May 3 00:00 gflags_validators.py
-rw-r--r-- 1 root root 7771 May 3 00:00 gflags_validators.pyc
drwxr-xr-x 3 root root 4096 May 3 00:00 google
drwxr-xr-x 2 root root 4096 May 3 00:00 google_api_python_client-1.2-py2.7.egg-info
drwxr-xr-x 2 root root 4096 May 3 00:00 google_apputils-0.4.0-py2.7.egg-info
-rw-r--r-- 1 root root 307 May 3 00:00 google_apputils-0.4.0-py2.7-nspkg.pth
drwxrwxr-x 6 app_name users 4096 May 2 12:13 gunicorn
drwxrwxr-x 2 app_name users 4096 May 2 12:13 gunicorn-18.0-py2.7.egg-info
drwxr-xr-x 4 root root 4096 May 2 23:20 httplib2-0.9-py2.7.egg
drwxrwxr-x 2 app_name users 4096 May 2 12:10 _markerlib
drwxr-xr-x 3 root root 4096 May 2 23:40 oauth2
drwxr-xr-x 2 root root 4096 May 2 23:40 oauth2-1.5.211-py2.7.egg-info
drwxr-xr-x 2 root root 4096 May 3 00:00 oauth2client
-rw-r--r-- 1 root root 111741 May 2 23:20 oauth2client-1.2-py2.7.egg
drwxrwxr-x 6 app_name users 4096 May 2 12:10 pip
drwxrwxr-x 2 app_name users 4096 May 2 12:10 pip-1.5.4.dist-info
-rw-rw-r-- 1 app_name users 99605 May 2 12:10 pkg_resources.py
-rw-rw-r-- 1 app_name users 106608 May 2 12:10 pkg_resources.pyc
drwxr-xr-x 2 root root 4096 May 3 00:00 python_dateutil-1.5-py2.7.egg-info
drwxr-xr-x 2 root root 4096 May 3 00:00 python_gflags-2.0-py2.7.egg-info
drwxr-xr-x 3 root root 4096 May 3 00:00 pytz
drwxr-xr-x 2 root root 4096 May 3 00:00 pytz-2014.2-py2.7.egg-info
drwxrwxr-x 2 app_name users 4096 May 2 12:22 setproctitle-1.1.8-py2.7.egg-info
-rwxrwxr-x 1 app_name users 53539 May 2 12:22 setproctitle.so
drwxrwxr-x 4 app_name users 4096 May 2 12:10 setuptools
drwxrwxr-x 2 app_name users 4096 May 2 12:10 setuptools-2.2.dist-info
drwxr-xr-x 3 root root 4096 May 2 23:43 simplejson
drwxr-xr-x 2 root root 4096 May 2 23:43 simple_json
drwxr-xr-x 2 root root 4096 May 2 23:43 simple_json-1.1-py2.7.egg-info
drwxr-xr-x 2 root root 4096 May 2 23:43 simplejson-3.4.1-py2.7.egg-info
-rw-r--r-- 1 root root 18568 May 3 00:00 table_formatter.py
-rw-r--r-- 1 root root 25967 May 3 00:00 table_formatter.pyc
drwxr-xr-x 2 root root 4096 May 2 23:40 tests
drwxr-xr-x 2 root root 4096 May 3 00:00 uritemplate
如果我进入 oauthclient2 ,我会看到appengine.py:
(app_name)root@readrboard-events:/webapps/app_name/lib/python2.7/site-packages/oauth2client# l
total 388
-rw-r--r-- 1 root root 1044 May 3 00:00 anyjson.py
-rw-r--r-- 1 root root 536 May 3 00:00 anyjson.pyc
-rw-r--r-- 1 root root 32524 May 3 00:00 appengine.py
-rw-r--r-- 1 root root 37284 May 3 00:00 appengine.pyc
-rw-r--r-- 1 root root 44282 May 3 00:00 client.py
-rw-r--r-- 1 root root 48404 May 3 00:00 client.pyc
-rw-r--r-- 1 root root 4405 May 3 00:00 clientsecrets.py
-rw-r--r-- 1 root root 4440 May 3 00:00 clientsecrets.pyc
-rw-r--r-- 1 root root 10233 May 3 00:00 crypt.py
-rw-r--r-- 1 root root 11970 May 3 00:00 crypt.pyc
-rw-r--r-- 1 root root 3833 May 3 00:00 django_orm.py
-rw-r--r-- 1 root root 5482 May 3 00:00 django_orm.pyc
-rw-r--r-- 1 root root 3160 May 3 00:00 file.py
-rw-r--r-- 1 root root 4291 May 3 00:00 file.pyc
-rw-r--r-- 1 root root 3038 May 3 00:00 gce.py
-rw-r--r-- 1 root root 3207 May 3 00:00 gce.pyc
-rw-r--r-- 1 root root 213 May 3 00:00 __init__.py
-rw-r--r-- 1 root root 416 May 3 00:00 __init__.pyc
-rw-r--r-- 1 root root 3227 May 3 00:00 keyring_storage.py
-rw-r--r-- 1 root root 3892 May 3 00:00 keyring_storage.pyc
-rw-r--r-- 1 root root 11379 May 3 00:00 locked_file.py
-rw-r--r-- 1 root root 12863 May 3 00:00 locked_file.pyc
-rw-r--r-- 1 root root 13935 May 3 00:00 multistore_file.py
-rw-r--r-- 1 root root 16386 May 3 00:00 multistore_file.pyc
-rw-r--r-- 1 root root 5548 May 3 00:00 old_run.py
-rw-r--r-- 1 root root 4981 May 3 00:00 old_run.pyc
-rw-r--r-- 1 root root 8344 May 3 00:00 tools.py
-rw-r--r-- 1 root root 8723 May 3 00:00 tools.pyc
-rw-r--r-- 1 root root 5670 May 3 00:00 util.py
-rw-r--r-- 1 root root 5853 May 3 00:00 util.pyc
-rw-r--r-- 1 root root 3368 May 3 00:00 xsrfutil.py
-rw-r--r-- 1 root root 2928 May 3 00:00 xsrfutil.pyc
1)我做错了什么?为什么没有加载appengine?
2)我找不到另一种方法来断言我的Google API凭据。如何使用我的crds连接到Google API以从非Google App Engine服务器查询BigQuery?
答案 0 :(得分:0)
“credentials = appengine.AppAssertionCredentials(scope = _SCOPE)”这一行永远不会在app引擎之外运行。 App Assertion凭据仅适用于在app引擎或dev_appserver上运行的应用。
要在不使用app断言的情况下获取凭据,您可以通过Cloud Console(https://developers.google.com/console/help/new/#serviceaccounts)创建服务帐户,并使用oauth2client.client.SignedJwtAssertionCredentials获取它们。这些凭据可以在以前使用过应用程序断言凭据的任何地方使用。