Unable to authenticate request through Cloudflare Python Module

For Workers & Pages, what is the name of the domain?

Not related with workers or Pages

What is the issue or error you’re encountering

cloudflare.BadRequestError: Error code: 400 - {‘success’: False, ‘errors’: [{‘code’: 10001, ‘message’: ‘Unable to authenticate request’}]}

What steps have you taken to resolve the issue?

Hello,

I am following Cloudflare documentation for the first time to get DNS records inside of DNS Zones: Cloudflare API | DNS › Records › list

As a start I have installed cloudflare Python Module (4.0.0), but when I try to run that script given on Cloudflare API documentation, it gives me an error, and I really don’t know what can be, since I have been using ‘requests’ with cloudflare queries with an API Key that has real-only for all permissions (set on my account), and have been able to get data, but with that python module I am getting:

cloudflare.BadRequestError: Error code: 400 - {‘success’: False, ‘errors’: [{‘code’: 10001, ‘message’: ‘Unable to authenticate request’}]}

This is the code I am using:


from cloudflare import Cloudflare

ZONE_ID = ‘XXXX’
CLOUDFLARE_EMAIL = ‘XXXX’
CLOUDFLARE_API_KEY = ‘XXXX’

client = Cloudflare(
api_email=CLOUDFLARE_EMAIL
api_key=CLOUDFLARE_API_KEY
)
page = client.dns.records.list(
zone_id=ZONE_ID,
)
page = page.result[0]
print(page)


Have also tried with just ‘user_service_key’, taking into account that was using just an API KEY to auth before over the other method, but got the same error.

I am trying to use this python module because it will be a lot more easier for me to work with.

I am felling a bit lost here … anyone here know what can be? or had the same issue?

Hi there,

Did you create a API token or retrieved the API key? For the token you can use api_token parameter:

client = Cloudflare(
    api_token="<read all resources api token>"
)

I’ve been using a script for the last 7/8 years to deploy work loads to Kubernetes. The script automates the injection of DNS records to Cloudflare using API calls as opposed to Python. Interestingly, it is failing with the same error message as the OP, specifically:

curl -s -X GET 'https://api.cloudflare.com/client/v4/zones/REDACTED/dns_records?type=A&name=REDACTED' -H 'X-Auth-Email: "REDACTED"' -H 'X-Auth-Key: REDACTED' -H 'Content-Type: application/json'
{"success":false,"errors":[{"code":10001,"message":"Unable to authenticate request"}]}

This is just a check to ensure the record is absent before I inject.

Seems something has recently gone awry somewhere. I was about to get Postman out to verify further but saw this. I’ll still verify and update here with more info.

Well, well. It appears this post being recent skewed my thinking. One thing I forgot to mention in my original post above is that although I’ve been using this script for >7 years on AWS, I’m now evaluating AKS and there was a slight change in one of the config files. Specifically, someone (nameless to protect the incompetent) modified the config file and added "'s around the CF account email address. Postman came to the rescue as always.

My ramblings can be safely ignored!