'GetTable' failed with status code 'Unauthorized'

Recently, one of our business users had left the firm, and handed over the canvas apps he was developing to his colleagues. There was one canvas app with flows that update SharePoint list. He had shared the app .zip file with the team and told, it's a completed app and just needs to import to the target environment after creating the SharePoint list. So, the team didn't bother much to collect further details about the source tenant where this was developed. He had created the app on a trail tenant which was tied with his account and was lost as soon as he left the firm.

But, the business users were unable to import the app and came to us with the below error.

 Flow save failed with code 'DynamicOperationRequestClientFailure' and message 'The dynamic operation request to API 'sharepointonline' operation 'GetTable' failed with status code 'Unauthorized'.  This may indicate invalid input parameters. Error response: { "error_description": "Exception of type 'Microsoft.IdentityModel.Tokens.AudienceUriValidationFailedException' was thrown." }'. 

The Issue

The issue here was the SharePoint site dependency. The definition.js files of the Power Automate flows had references to the old site and the current user does not have access to the original site, hence the flow was failing with error 'Unauthorized' even though, SharePoint connection was created in the target environment.

The Fix

The fix was to edit the definition.js file and replace the SharePoint URL and List ID with the new site information. The definition.js file is in the exported zip file. We had to replace all the occurrence of the URL, list ID (defined under table) and view ID, and saved the definition.js file and copied it to the zip file. Importing the updated zip file worked.

{"dataset":"https://***.sharepoint.com/","table":"d2**826-e05a-4972-be****8","view":"**-27f2-4ace-a289-ef41a**"}

The Right Way

This is a known issue when moving PowerApps/ Power Automate to a new tenant. This behaviour is not consistent as some flows were imported with no errors and others were rejected due to Unauthorized exception on GetTable().

One way to avoid this issue, is to use variables instead of directly selecting the SharePoint site, list and view in the SharePoint actions inside your flow.

Another option is to use environment variables in your flow. This will also help us avoid direct SharePoint references in your flow.

Hope this helps.

If you know any alternate option to handle this, please let us know in the comments.