@Kevin van der Gugten - Thanks for the question and using MS Q&A platform.
Based on the error message you provided, it looks like your application does not have the appropriate rights to use the API. You mentioned that you have already given your application the SQL DB Contributor/SQL Server Contributor roles, but it still doesn't work.
One thing you can try is to check if the scope of your application is correct. The error message indicates that the scope might be invalid. Make sure that the scope of your application is set to '/subscriptions/{subscription}/resourceGroups/{resource_group}/providers/Microsoft.Sql/servers/{sql_server}/databases'.
Another thing you can try is to refresh your credentials. The error message suggests that if access was recently granted, you should refresh your credentials. You can regenerate the client credentials and try again.
Regarding your thought about the sleep() function causing the issue, it is possible that sending too many requests in a short amount of time could cause your application to be flagged. However, it is difficult to say for sure without more information.
I hope this helps! Let me know if you have any other questions.