Authentication not granted for service principal token in MS Fabric API using Python
I am trying to connect to OneLake API in Microsoft Fabric using Python in VScode.
So far I have
* Registered an app in Azure with these API permissions
* Then created a secret for my service principal
* Then I try to get the token with this function, using azure.identity:
from azure.identity import ClientSecretCredential, AuthenticationRequiredError
def get_access_token(app_id, client_secret, directory_id):
try:
# Create the ClientSecretCredential using the provided credentials
credential = ClientSecretCredential(
client_id=app_id,
client_secret=client_secret,
tenant_id=directory_id
#scope="https://storage.azure.com/.default"
)
# Use the credential to get the access token
token = credential.get_token("https://storage.azure.com/.default").token
return token, credential
except AuthenticationRequiredError as e:
print("Authentication failed. Please check your credentials.")
raise e
except Exception as e:
print("An error occurred while getting the access token:")
print(str(e))
raise e
access_token, credential = get_access_token(app_id, client_secret, directory_id)
It seems I get the token fine and all. But there is something wrong with the permissions or scope or access. Because when I run this function to check for connection i get status code 400
def check_connection_with_onelake(access_token):
base_url = "https://onelake.dfs.fabric.microsoft.com/9c3ffd43-b537-4ca2-b9ba-0c59d0094033/Files/sample?resource=file"
token_headers = {
"Authorization": "Bearer " + access_token
}
try:
response = requests.put(base_url, headers=token_headers)
if response.status_code == 200:
print("Connection with OneLake is successful.")
else:
print("Failed to connect with OneLake. Status code:", response.status_code)
except requests.exceptions.RequestException as e:
print("An error occurred while checking the connection:", str(e))
# Assuming 'access_token' is already defined and contains a valid access token
check_connection_with_onelake(access_token)
* I also added the app's service principal to users in the Fabric workspace as an admin
Where am I missing access and how do I grant the correct access?
references:
https://learn.microsoft.com/en-us/fabric/onelake/onelake-access-api
https://amitchandak.medium.com/on-premise-python-code-to-local-sql-server-data-to-microsoft-fabric-lakehouse-using-token-d15b8795e349
0 comments:
Post a Comment
Thanks