无效 API key/application 对–Clarifai

Invalid API key/application pair–Clarifai

我正在阅读文档并想尝试 pycharm 中的 api。所以我复制了代码,它告诉我我有一个“无效的 API key/application 对”。我直接从我在 https://portal.clarifai.com/ 上制作的应用程序中复制了我的 Api 密钥并将其放入。

我的代码实际上是一个精确的副本,除了 api 键被删除我直接从我的应用程序复制被删除,我 运行 它在 pycharm。

`

from clarifai_grpc.channel.clarifai_channel import ClarifaiChannel
from clarifai_grpc.grpc.api import resources_pb2, service_pb2, service_pb2_grpc
from clarifai_grpc.grpc.api.status import status_pb2, status_code_pb2

# Construct one of the channels you want to use
channel = ClarifaiChannel.get_json_channel()
channel = ClarifaiChannel.get_insecure_grpc_channel()

# Note: You can also use a secure (encrypted) ClarifaiChannel.get_grpc_channel() however
# it is currently not possible to use it with the latest gRPC version

stub = service_pb2_grpc.V2Stub(channel)

# This will be used by every Clarifai endpoint call.
metadata = (('authorization', 'Key {9f3d8b8ea01245e6b61c2a1311622db1}'),)
# Insert here the initialization code as outlined on this page:
# https://docs.clarifai.com/api-guide/api-overview/api-clients#client-installation-instructions

post_inputs_response = stub.PostInputs(
    service_pb2.PostInputsRequest(
        inputs=[
            resources_pb2.Input(
                data=resources_pb2.Data(
                    image=resources_pb2.Image(
                        url="https://samples.clarifai.com/metro-north.jpg",
                        allow_duplicate_url=True
                    )
                )
            )
        ]
    ),
    metadata=metadata
)

if post_inputs_response.status.code != status_code_pb2.SUCCESS:
    raise Exception("Post inputs failed, status: " + post_inputs_response.status.description)

`

能否请您尝试生成一个新的 API 密钥并尝试一下。

Philip,您的元数据声明不正确。您没有将 API 密钥放在 {}

# This will be used by every Clarifai endpoint call.
metadata = (('authorization', 'Key {9f3d8b8ea01245e6b61c2a1311622db1}'),)

末尾多了一个},请调整成这样

metadata = (('authorization', 'Key 9f3d8b8ea01245e6b61c2a1311622db1'),)