Search code examples
pythonapixmlhttprequesthttp-headerspostman

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.

enter image description here

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(

Solution

  • 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)