It’s because of fact that you don’t have permission to access org using API.
If you are using developer console:
Quick way to fix it to give permission “Use any API client” using profile or permission set.
NOTE: If you do not find “Use any API client” then try to enable the permission “API Enabled”.
If you are using connected App:
This is happening because of connected app is not configured correctly.
API (Enable OAuth Settings)‘ panel, move ‘
Access and manage your data (api)‘ option from left to right of
Selected OAuth Scopes field and then save the setting.
Wait for few minute and then try. It should work.