从Google Cloud yalm python应用程序中检索firebase数据库数据

时间:2018-04-08 10:13:50

标签: python firebase google-cloud-functions firebase-admin google-cloud-python

我正在构建一个每1分钟启动一次的Google云端功能。目的是从我的数据库(firebase)获取数据,并在帖子的日期优于一小时前删除此数据。

我正在考虑制作firebase云功能,并在一小时后使用settimeout功能。不幸的是,这是不可能的。所以我决定使用gcloud的cron功能。

这是我的main.py。

import webapp2
from gcloud import datastore
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Fetch the service account key JSON file contents
cred = credentials.Certificate('key.json')

try:
    # Initialize the app with a service account, granting admin privileges
    firebase_admin.initialize_app(cred, {
        'databaseURL': 'https://#########.firebaseio.com/'
    })
except:
    print('no need for init firebase')
# As an admin, the app has access to read and write all data, regradless of Security Rules
ref = db.reference('/')



class HourCronPage(webapp2.RequestHandler):
    def get(self):
        ref.child("test").delete()


app = webapp2.WSGIApplication([
    ('/timer', HourCronPage),
    ], debug=True)

以下是我从gcloud控制台获得的错误,我不知道如何解决这个问题:

('Connection broken: IncompleteRead(14 bytes read)', IncompleteRead(14 bytes read)) (/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py:1552)
Traceback (most recent call last):
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/main.py", line 25, in get
    ref.child("test").delete()
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/firebase_admin/db.py", line 282, in delete
    self._client.request('delete', self._add_suffix())
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/firebase_admin/db.py", line 774, in request
    return super(_Client, self).request(method, url, **kwargs)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/firebase_admin/_http_client.py", line 84, in request
    resp = self._session.request(method, self._base_url + url, **kwargs)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/auth/transport/requests.py", line 198, in request
    self._auth_request, method, url, request_headers)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/auth/credentials.py", line 121, in before_request
    self.refresh(request)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/oauth2/service_account.py", line 322, in refresh
    request, self._token_uri, assertion)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/oauth2/_client.py", line 145, in jwt_grant
    response_data = _token_endpoint_request(request, token_uri, body)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/oauth2/_client.py", line 106, in _token_endpoint_request
    method='POST', url=token_uri, headers=headers, body=body)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/google/auth/transport/requests.py", line 124, in __call__
    six.raise_from(new_exc, caught_exc)
  File "/base/data/home/apps/s~instavice-application/20180408t195049.408875809865949267/lib/six.py", line 737, in raise_from
    raise value
TransportError: ('Connection broken: IncompleteRead(14 bytes read)', IncompleteRead(14 bytes read))

有人有想法吗?

提前多谢你们。

1 个答案:

答案 0 :(得分:1)

您可能需要在App Engine环境中设置相同的其他依赖项(至少为ssl):https://google-auth.readthedocs.io/en/latest/user-guide.html#the-app-engine-standard-environment