Invalid Api key using RestServer

8.3k views Asked by At

I am using the codeigniter rest server api library.

When I enter http://localhost/projects/myapi/key/index_put.php and hit enter gives me the following error:

<xml>
<status>0</status>
<error>Invalid API Key</error>
</xml>

When I give a dummy string in the url, like:

http://localhost/projects/myapi/key/index_put.php?X-API-KEY=asldfj9alsdjflja97979797997

I get the same problem. Any idea?

index_put.php:

public function index_put() {
        // Build a new key
        $key = self::_generate_key();
        // If no key level provided, give them a rubbish one
        $level = $this->put('level') ? $this->put('level') : 1;
        $ignore_limits = $this->put('ignore_limits') ? $this->put('ignore_limits') : 1;

        // Insert the new key
        if (self::_insert_key($key, array('level' => $level, 'ignore_limits' => $ignore_limits))) {
            $this->response(array('status' => 1, 'key' => $key), 201); // 201 = Created
        } else {
            $this->response(array('status' => 0, 'error' => 'Could not save the key.'), 500); // 500 = Internal Server Error
        }
    }
3

There are 3 answers

1
Abdul Manan On BEST ANSWER

i faced the same issue .don't mention put/get/post in URL ,RestServer itself recognizes the request nature base on parameter you pass two step required to solve your problem .

1st Step :

http://localhost/projects/myapi/key/index_put.php

must change to :

http://localhost/projects/myapi/key/index.php

2nd step:

create an api kay using sha1(max 40 character) in the keys table (table structure show in config/rest.php file), enter 1 in is_private_key field and ::1 in ip_address field. create the record ,and check it again .

0
diego cantero On

This works for me:

in file application/config/rest.php search section auth_override_class_method and add this line:

$config['auth_override_class_method']['key']['*'] = 'basic';

enter image description here

0
CodeOneOnOne On

This question is old but for those who finds this, the answer is:

The library https://github.com/chriskacerguis/codeigniter-restserver when using PUT method, the API KEY should be in the put header variables as x-www-form-urlencoded type.

Use google chrome postman and fill like image below: enter image description here