Search code examples
pythonapipycharmclarifai

Invalid API key/application pair–Clarifai


I was reading the documentation and wanted try out the api in pycharm. So I copied the code and it told me that I had an "Invalid API key/application pair". I copied my Api key straight from my app I made on https://portal.clarifai.com/ and put it in.

My code literally an exact copy except api key was deleted I copied straight from my app was deleted, and I running it on 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)

`


Solution

  • Philip, your metadata declaration is not correct. You don't put your API key within {}

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

    There is an extra } towards the end, please adjust it to this

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