Google 机器学习引擎和 Python 数据存储 API、'Forbidden: 403 Request had insufficient authentication scopes.'

Google ML Engine and Python Datastore API, 'Forbidden: 403 Request had insufficient authentication scopes.'

我是 运行 Google 机器学习引擎上的 TensorFlow 模型。模型训练完成后,我想将包含结果的 JSON 字符串存储到 Datastore。为此,我使用以下内容:

from gcloud import datastore

def put_json_into_datastore(json_str, project_id, entity_type):
    """
    Store json string in Datastore
    """
    # Instantiate the client to the project
    datastore_client = datastore.Client(project_id)
    # The name/ID for the new entity
    name = str(datetime.datetime.now())
    # The Cloud Datastore key for the new entity
    entity_key = datastore_client.key(entity_type, name)
    # Prepare the new entity
    entity = datastore.Entity(key=entity_key)
    # Get the json string into the entity
    entity.update(json_str)
    # Put the entity into Datastore
    datastore_client.put(entity)

不过,我收到错误 'Forbidden: 403 Request had insufficient authentication scopes.' 这是完整的错误跟踪:

Traceback (most recent call last): File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main "main", fname, loader, pkg_name) File "/usr/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals File "/root/.local/lib/python2.7/site-packages/trainer/train.py", line 243, in FLAGS.entity_type) File "/root/.local/lib/python2.7/site-packages/trainer/data_helpers.py", line 253, in put_json_into_datastore datastore_client.put(entity) File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/client.py", line 329, in put self.put_multi(entities=[entity]) File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/client.py", line 355, in put_multi current.commit() File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/batch.py", line 260, in commit self._commit() File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/batch.py", line 243, in _commit self.project, self._commit_request, self._id) File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py", line 342, in commit _datastore_pb2.CommitResponse) File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py", line 124, in _rpc data=request_pb.SerializeToString()) File "/usr/local/lib/python2.7/dist-packages/gcloud/datastore/connection.py", line 98, in _request raise make_exception(headers, error_status.message, use_json=False) Forbidden: 403 Request had insufficient authentication scopes.

我是否需要在某处授予访问权限以便 ML 引擎访问 Datastore?

Cloud ML 服务的执行权限不足以访问 Datastore。解决此问题的一种方法是为有权访问 Cloud Datastore 的服务帐户上传凭据(例如 json 服务帐户密钥文件)。然后您可以使用它来获取能够访问 Datastore 的凭据。