Public Key: Error: 401 Client Error: Unauthenticated

When I tried to run this py script

import requests
from eth_account import Account

def address_to_public_key(address):
infura_api_key = “MY_API_KEY”
url = f"https://mainnet.infura.io/v3/{infura_api_key}"

try:
    response = requests.post(url, json={"jsonrpc":"2.0","method":"eth_getTransactionCount","params":[address, "latest"],"id":1})
    response.raise_for_status()
    transaction_count = int(response.json()['result'], 16)
    
    # If there are transactions, we can retrieve the public key
    if transaction_count > 0:
        public_key = Account.from_address(address).publickey().to_hex()
        return public_key
    else:
        return "No transactions found for the given address"
except requests.exceptions.RequestException as e:
    return f"Error: {e}"

your_address = “MY_ETH_ ADDRESS”
public_key = address_to_public_key(your_address)

print(“Public Key:”, public_key)

It’s showing me this error
Public Key: Error: 401 Client Error: Unauthorized for url: https://mainnet.infura.io/v3/here is my API key

I crated a new api key, please help me

1 Like

hey, can you please open a support ticket and let us know the api key you’re having issues with ? https://support.infura.io/

This topic was automatically closed after 30 days. New replies are no longer allowed.