Creating file in workspace with REST API:
PUT https://onelake.dfs.fabric.microsoft.com/{workspace}/{item}.{itemtype}/Files/sample?resource=file
Status code:401 Unauthorized
Error: Authentication Failed with Audience validation failed for audience 'https://analysis.windows.net/powerbi/api'
The application has all Power Bi permissions with consent still can't create a file.
Similar problem found here with no solution Creating OneLake files through REST API calls fail... - Microsoft Fabric Community
The error usually occurs if you are generating access token with Power BI scope and using it to create file in One Lake workspace.
I generated one access token using client credentials flow with Power Bi scope like this:
POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id: appId
client_secret: secret
scope: https://analysis.windows.net/powerbi/api/.default
Response:
When I used this token to create file in One Lake workspace, I too got same error as below:
PUT https://onelake.dfs.fabric.microsoft.com/FabricWS/Lakehouse01.Lakehouse/Files/sample.txt?resource=file
Response:
To resolve the error, you need to generate access token with Storage scope and use that token to create file in workspace.
In the registered application, I added Storage API permission
as below:
Now, I generated access token with Storage scope using client credentials flow via Postman like this:
POST https://login.microsoftonline.com/tenantId/oauth2/v2.0/token
grant_type:client_credentials
client_id: appId
client_secret: secret
scope: https://storage.azure.com/.default
Response:
When I used this token to create file in One Lake workspace, I got 201 Created
response like below:
PUT https://onelake.dfs.fabric.microsoft.com/FabricWS/Lakehouse01.Lakehouse/Files/sample.txt?resource=file
Response:
To confirm that, I checked the same in Portal where file created successfully in workspace like this: