RavenPack API_key可在postman中使用,但不能在python代码中使用

mlmc2os5  于 2022-11-07  发布在  Postman
关注(0)|答案(2)|浏览(149)

我正在尝试使用此存储库https://github.com/RavenPack/python-api
奇怪的是,API与POSTMAN(一种用于自动化测试API开发和HTTP调用的工具)完美地配合工作。
下面是代码片段

from ravenpackapi import RPApi
from ravenpackapi import Dataset

api = RPApi(
    api_key="_________" //correct api_key was intentionally removed for the post
)

ds = api.create_dataset(
    Dataset(
        name="New Dataset",
        filters={
            "relevance": {
                "$gte": 90
            }
        },
    )
)

print("Dataset created", ds)

以下是错误消息。

**我重复一遍,相同的API密钥在相同的设备和网络上与postman一起工作。**只是他们的python库让我很难接受。

Exception has occurred: APIException
Got an error 401: body was '{"endpoint":"datasets","errors":[{"type":"UnauthorizedError","reason":"Unauthorized: Must supply a valid API key"}]}'
  File "C:\Users\XYZ\Documents\Python\RavenPackAPI.py", line 8, in <module>
    ds = api.create_dataset(
zf9nrax1

zf9nrax11#

问题可能是您没有指向正确的API群集。API密钥可能只适用于以前的产品版本。
要指向Edge群集,请使用以下代码设置您的API:

from ravenpackapi import RPApi

api = RPApi(product="edge")
9gm1akwq

9gm1akwq2#

https://github.com/RavenPack/python-api/blob/master/ravenpackapi/examples/create_dataset_edge.py
以下代码是解决方案:^^^我使用的是在内部调用RPA群集的旧代码片段:

from ravenpackapi import RPApi
from ravenpackapi import Dataset

# api = RPApi(api_key="L3KOJd7sjL7ZPsWXt4geSy")

api = RPApi(api_key="L3KOJd7sjL7ZPsWXt4geSy", product="edge")

ds = api.create_dataset(
    Dataset(
      **{
            "name": "Edge Dataset",
            "product": "edge",
            "product_version": "1.0",
            "frequency": "granular",
            "fields": [
                "timestamp_utc", "rp_document_id", "rp_entity_id", "entity_type", "entity_name",
                "country_code", "event_relevance", "entity_sentiment", "event_sentiment", "topic", "group"
            ],
            "filters": {
                "$and": [
                    {"event_relevance": {"$gte": 90}},
                    {"country_code": {"$in": ["GB"]}},
                    {"event_sentiment": {"$nbetween": [-0.5, 0.5]}}
                ]
            },
        }
    )
)

print("Dataset created", ds)

相关问题