简体   繁体   中英

RavenPack API_key is working in postman but NOT in python code

I am tryin to work with this repo https://github.com/RavenPack/python-api

Weirdly enough, the API is working perfectly with POSTMAN (it's a tool used to automate testing API development and HTTP calls)

Here's the code snippet

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)

Here is the error message.

在此处输入图像描述

I repeat the same API key is working with postman on the same device and network. It's just that their python library is giving me a hard time.

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(

The issue may be that you are not pointing to the correct API cluster. The API Key may only be entitled to the previous product version.

To point to the Edge cluster - set your API with the following code:

from ravenpackapi import RPApi

api = RPApi(product="edge")

https://github.com/RavenPack/python-api/blob/master/ravenpackapi/examples/create_dataset_edge.py

This code is the solution^^^ I was using an old code snippet that called the RPA cluster internally:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM