0

我尝试使用邮递员的HTTP API 方法在我部署的一个模型上调用预测请求,我得到了这个作为响应:

{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity /sign-in/web/devconsole-project .", "status": "UNAUTHENTICATED" } }

我意识到我们需要身份验证,所以我尝试使用 Firebase Cloud Functions 进行相同的 HTTP 调用,但仍然得到与上述相同的响应。我做了一些挖掘,发现了所有可以与云功能一起使用的服务,我在其中看到了 ML Engine。

我在模型的权限选项卡中将 Cloud Functions 服务帐户添加为 ML Engine Owner,希望它添加 API 调用所需的身份验证,但仍然无法正常工作。

我不想同样使用 cli 或 python-client-library,目的是使这项工作无服务器。

谁能帮助我了解为什么会发生这种情况,或者我还能如何对预测请求进行 HTTP 调用?

谢谢。

4

2 回答 2

2

您是否为 http 请求设置了授权标头?授权:承载

这里有一些云机器学习引擎的文档: https ://cloud.google.com/ml-engine/docs/access-control

另一个 Google Cloud 功能的文档(概念相同): https ://cloud.google.com/vision/docs/auth#using_a_service_account

顺便说一句,以防万一,函数不是必须的,我相信您可以从您的本机应用程序调用,并在标头中传递 ApiKey。

于 2018-02-13T07:32:22.877 回答
0

对我来说,它的工作原理如下。在同一个谷歌云项目中,我部署了 ML 模型(ML 平台 -> 模型)和 Cloud Function,我创建了角色为“Cloud ML Developer”的服务帐户。必须在 Cloud Function 配置中提供创建的服务帐户名称:

在此处输入图像描述 云函数代码:main.py

googleapiclient import discovery
import json

def run(request):
  request_json = request.get_json()
  if request.args and 'message' in request.args:
    return request.args.get('message')
  elif request_json and 'message' in request_json:
    return request_json['message']
  elif request_json and hasattr(request_json, "__len__"):
    res = ml_call(prepare_frame(request_json))
    return json.dumps(res) 

  else:
    return f'Request error'

def ml_call(req):    
  PROJECT = 'test_proj'
  MODEL_NAME = 'test_name'
  MODEL_VERSION = 'test_ver'
  parent = 'projects/{}/models/{}/versions/{}'.format(PROJECT, MODEL_NAME, MODEL_VERSION)

  # Build a representation of the Cloud ML API.
  ml = discovery.build('ml', 'v1')

  # Create a dictionary with the fields from the request body.
  data = {'instances': [{'input_image': req}]}

  # Create a request 
  request = ml.projects().predict(name = parent, body = data) 

  response = request.execute()
  return response

def prepare_frame(xxx):
...
  return x

要求.txt:

google-api-python-client
于 2019-04-24T10:02:33.307 回答