Invalid API Key: Troubleshooting with RestServer
Encountering an "Invalid API Key" error while using RestServer in CodeIgniter? Here are comprehensive solutions to address this issue:
1. Correct URL Format
Ensure you are using the proper URL format for your API requests. RestServer automatically detects the request's nature based on the parameters passed. However, you must make the following change:
Incorrect URL:
http://localhost/projects/myapi/key/index_put.php
Corrected URL:
http://localhost/projects/myapi/key/index.php
2. API Key Creation and Configuration
- Create an API key using
sha1
(max 40 characters) in the keys table (table structure is defined inconfig/rest.php
). - Set
is_private_key
to 1 andip_address
to::1
. - Save the record and check again.
Additional Solutions:
Using x-www-form-urlencoded Header
When using the PUT method, the API key should be in the PUT header variables as x-www-form-urlencoded type. Use a tool like Google Chrome Postman to fill in the necessary details.
Adding 'auth_override_class_method' Configuration
- Open
application/config/rest.php
. - Locate the
auth_override_class_method
section. - Add the following line:
$config['auth_override_class_method']['key']['*'] = 'basic';
Make sure to modify the configuration based on your specific API requirements.