Solving authorization problems with the API is hard, because obviously you dont want anyone else to know your credentials, and that makes it impossible for anyone else to reproduce your issue.
The credentials are different between LIVE and DEMO. The username, password and API key are all different. In addition, authentication itself is difficult with the API if you are new to it.
If you think you have created an API key correctly, but your credentials don't work, it's probably because you think you have a LIVE account, but it is DEMO. Or the other way around. Or, you are mixing up username/password/API key between LIVE and DEMO. The system will not allow you to create a DEMO API key until you have funded your LIVE account. If you make too many incorrect authentication attempts, your account will be locked. If that happens, you need to contact support to get it unlocked.
I would recommend resolving authentication issues with the REST API Companion
https://labs.ig.com/sample-apps/api-rest-companion-release/index.html
That way you can be sure the API methods will be called correctly, and you can concentrate on getting your credentials and API keys recorded properly. Only move on to actually calling API code once you are sure you have resolved the credentials issues.
Also you need to be aware of the issue discussed here: