403权限被拒绝从PubSub访问AutoML

时间:2018-09-04 04:21:57

标签: python google-cloud-platform publish-subscribe google-cloud-pubsub google-cloud-automl

我正在尝试使用Google Cloud Platform AutoML使用Python来构建应用程序。我的总体代码流如下所示:

用户交互->发送到PubSub的数据->回调调用我的AutoML->结果

调用pubsub的代码段如下所示:

blob=blob+bytes(doc_type,'utf-8')
        publisher.publish(topic,blob)
        future=subscriber.subscribe(subscription,callback=callback)
        #flash("The object is "+future,'info')
        try:
            future.result()
        except Exception as ex:
            subscriber.close()

PubSub回调中:

def callback(message):
     new_message=message.data
     display_name,score=predict_value(new_message,"modelID","projectid",'us-central1')
     message.ack()

然后我的predict_value得到model_idproject id并计算region并执行预测。

在我不使用predict_value的情况下直接致电PubSub时,它工作正常。如果我这样做,将会收到以下错误:

google.api_core.exceptions.PermissionDenied: 403 Permission 'automl.models.predict' denied on resource 'projects/projectID/locations/us-central1/models/' (or it may not exist).

请帮助我解决问题

2 个答案:

答案 0 :(得分:2)

非常感谢您的所有回复。我已经使用下面的代码片段示例解决了该问题

def receive_messages_synchronously(project, subscription_name):
"""Pulling messages synchronously."""
# [START pubsub_subscriber_sync_pull]
# project           = "Your Google Cloud Project ID"
# subscription_name = "Your Pubsub subscription name"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
    project, subscription_name)

# Builds a pull request with a specific number of messages to return.
# `return_immediately` is set to False so that the system waits (for a
# bounded amount of time) until at lease one message is available.
response = subscriber.pull(
    subscription_path,
    max_messages=3,
    return_immediately=False)

ack_ids = []
for received_message in response.received_messages:
    print("Received: {}".format(received_message.message.data))
    ack_ids.append(received_message.ack_id)

# Acknowledges the received messages so they will not be sent again.
subscriber.acknowledge(subscription_path, ack_ids)
# [END pubsub_subscriber_sync_pull]

原因是创建的订阅使用拉取请求。我猜回调方法的概念主要用于“推送”,这可能是原因,因为我没有提供端点和令牌来发布消息。希望我的猜测是正确的。让我也知道您的看法。

答案 1 :(得分:0)

这可能是由于以下两个因素之一造成的:

  • 将请求发送到AutoML API时使用了无效的凭据-pubsub很可能在其他上下文中执行并且无法获取默认凭据

  • 无效的模型资源名称(确保正确)-应该类似于:“ projects / 12423534 / locations / us-central1 / models / 23432423”