【发布时间】:2020-09-19 03:36:33
【问题描述】:
我正在阅读文档并想在 pycharm 中试用 api。所以我复制了代码,它告诉我我有一个“无效的 API 密钥/应用程序对”。我直接从我在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 密钥并检查它是否有效,同时测试包括所有范围 docs.clarifai.com/getting-started/authentication/…
-
哦,我忘了从代码中删除我的密钥,所以我创建了一个新密钥并删除了旧密钥。
标签: python api pycharm clarifai