Is there any error in my code, why am i getting a 401 error with the Experian API

So, I am creating a web application, this is my first time using an API and connecting to external webserver, I am good at python and creating script but this was the first step to make sure I can get data then build the UX and all the other function, but this issue is stopping me. I rather use python then json is it even possible?

import requests
import json
import time

# Configuration
CLIENT_ID = "Rr6cUeNnxmVgXojKyhu7YABaXWYM6gyK"
CLIENT_SECRET = "Gzd3nVpVbY0A802I"
USERNAME = "[email protected]"
PASSWORD = "$01011Karfearai"
BASE_URL = "https://sandbox-us-api.experian.com"

# Global variables to store tokens
access_token = None
refresh_token = None

def request_access_token():
    global access_token, refresh_token
    url = f"{BASE_URL}/oauth2/v1/token"
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    data = {
        "username": USERNAME,
        "password": PASSWORD,
        "client_id": CLIENT_ID,
        "client_secret": CLIENT_SECRET,
        "grant_type": "password"
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        tokens = response.json()
        access_token = tokens.get("access_token")
        refresh_token = tokens.get("refresh_token")
        print("Access Token:", access_token)
        print("Refresh Token:", refresh_token)
    else:
        print("Failed to get access token. Status code:", response.status_code)
        print("Response:", response.json())

def refresh_access_token():
    global access_token, refresh_token
    url = f"{BASE_URL}/oauth2/v1/token"
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    data = {
        "client_id": CLIENT_ID,
        "client_secret": CLIENT_SECRET,
        "grant_type": "refresh_token",
        "refresh_token": refresh_token
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        tokens = response.json()
        access_token = tokens.get("access_token")
        refresh_token = tokens.get("refresh_token")
        print("New Access Token:", access_token)
        print("New Refresh Token:", refresh_token)
    else:
        print("Failed to refresh token. Status code:", response.status_code)
        print("Response:", response.json())

def revoke_token(token):
    url = f"{BASE_URL}/oauth2/v1/revokeToken"
    headers = {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    }
    data = {
        "client_id": CLIENT_ID,
        "client_secret": CLIENT_SECRET,
        "token": token
    }

    response = requests.post(url, headers=headers, data=json.dumps(data))

    if response.status_code == 200:
        print("Token successfully revoked.")
    else:
        print("Failed to revoke token. Status code:", response.status_code)
        print("Response:", response.json())

def make_api_call():
    global access_token
    url = f"{BASE_URL}/business/score/v2/directors"
    headers = {
        'Accept': 'application/json',
        'Authorization': f'Bearer {access_token}'
    }

    response = requests.get(url, headers=headers)

    if response.status_code == 200:
        print("API response:", response.json())
    else:
        print("Failed to fetch data. Status code:", response.status_code)
        print("Response:", response.json())

# Main workflow
if __name__ == "__main__":
    # Step 1: Request Access Token
    request_access_token()

    # Simulate waiting for token expiration
    time.sleep(5)  # Wait for 5 seconds (in real use, wait until token is about to expire)

    # Step 2: Use Bearer Token
    make_api_call()

    # Step 3: Refresh Access Token
    refresh_access_token()

    # Step 4: Revoke Token
    revoke_token(access_token)



Out come :
enter image description here

https://developer.experian.com/get-started#step-1

https://developer.experian.com/products/us/businesses/businesses-api-docs

I used chat GPT and now I am here on stack over flow hahahaha

New contributor

Karfear’s Softwear is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật