DataDog / datadogpy

The Datadog Python library
https://datadoghq.com/
Other
609 stars 303 forks source link

{"errors":[{"status":"400","title":"Bad Request","detail":"HTTP method is invalid"}]} #745

Closed tgy closed 1 year ago

tgy commented 1 year ago

Describe the bug

Can't query metrics with Python API

To Reproduce

Run the following Python code with a proper API key set as the api_key variable

import datetime
import os

from datadog_api_client import ApiClient, Configuration
from datadog_api_client.v1.api.metrics_api import MetricsApi
from dateutil.relativedelta import relativedelta

os.environ["DD_SITE"] = "datadoghq.com"
api_key = "<hidden>"
os.environ["DD_API_KEY"] = api_key
query = "service.marketplace_matching_algo.dependency_resolver.time.avg"
configuration = Configuration()
with ApiClient(configuration) as api_client:
    api_instance = MetricsApi(api_client)
    response = api_instance.query_metrics(
        _from=int((datetime.datetime.now() + relativedelta(days=-1)).timestamp()),
        to=int(datetime.datetime.now().timestamp()),
        query=f"{query}{{*}}",
    )
    print(response)

Results in following error

ForbiddenException: (403)
Reason: Forbidden
HTTP response headers: HTTPHeaderDict({'Date': 'Thu, 01 Dec 2022 11:19:37 GMT', 'Content-Type': 'application/js
on', 'Content-Length': '25', 'Connection': 'keep-alive', 'pragma': 'no-cache', 'cache-control': 'no-cache', 'x-
content-type-options': 'nosniff', 'strict-transport-security': 'max-age=15724800;', 'content-security-policy':
"frame-ancestors 'self'; report-uri https://logs.browser-intake-datadoghq.com/api/v2/logs?dd-api-key=pube4f163c
23bbf91c16b8f57f56af9fc58&dd-evp-origin=content-security-policy&ddsource=csp-report&ddtags=site%3Adatadoghq.com
", 'x-frame-options': 'SAMEORIGIN'})
HTTP response body: {'errors': ['Forbidden']}

Going to the link in the error gives

{"errors":[{"status":"400","title":"Bad Request","detail":"HTTP method is invalid"}]}

Expected behavior

I expect this API call to be valid and to return the queried metrics.

Environment and Versions (please complete the following information):

val ❯ pip freeze | grep datadog
datadog==0.43.0
datadog-api-client==2.6.0

val ❯ python --version
Python 3.10.6
therve commented 1 year ago

Hi,

This is the wrong repository, you're not in datadog-api-client-python but datadogpy. The error you're getting is a 403, which means it fails to authenticate properly. https://github.com/DataDog/datadog-api-client-python/blob/master/README.md#authentication has the instructions, or you can find them in the API documentation https://docs.datadoghq.com/api/latest/. Thanks