Approach seems to be correct considering fact that you have added already "added my own public IP address to the firewall" and given the required permission "key vault secret user" to app registration .
You will only need to use the client id, secret id and tenant id to get access token to authenticate and get the key vault secret. Setting up Api permission won't be needed.
As part of debugging, you can test below code from vs code with python SDK on accessibility. (Repro'd with a public resource though)
import requests
def get_access_token(tenant_id, client_id, client_secret):
url = f'https://login.microsoftonline.com/{tenant_id}/oauth2/v2.0/token'
payload = {
'grant_type': 'client_credentials',
'client_id': client_id,
'client_secret': client_secret,
'scope': 'https://vault.azure.net/.default'
}
response = requests.post(url, data=payload)
response.raise_for_status()
return response.json().get('access_token')
def get_secret(vault_name, secret_name, access_token):
url = f'https://{vault_name}.vault.azure.net/secrets/{secret_name}?api-version=7.0'
headers = {
'Authorization': f'Bearer {access_token}'
}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json().get('value')
def main():
tenant_id = "<tenantid>"
client_id = "<clientid"
client_secret = "<clientsecret>"
vault_name = '<keyvault name>'
secret_name = '<secretname>'
# Get access token
access_token = get_access_token(tenant_id, client_id, client_secret)
# Fetch secret from Key Vault
secret_value = get_secret(vault_name, secret_name, access_token)
print(f'The secret value is: {secret_value}')
if __name__ == '__main__':
main()
Please share error trace if you are still facing issue.
Reference
Please upvote my answer and say "yes" if it helped.
Thank you.