2012-02-03 106 views
8

我得到了庫的其餘部分完全工作,只是試圖生成api鍵,並通過ajax執行時拋出403禁止。CodeIgniter REST API庫Ajax PUT投擲403 Forbidden

({"status":false,"error":"Invalid API Key."})

我跟蹤它_remap REST_Controller下功能..彷彿IM調用網址是否有誤?

工作流程:user visits site1.com -> registers for account -> generates api key for their domain -> key recorded in db -> key displayed

下面的表格將是對他們site1.com註冊一個帳戶後,他們會點擊「生成密鑰」。

Ajax調用:在GitHub上

/** 
* Generate an API Key for Us to use 
*/ 

$("#submitGetApiKey").click(function(){ 
    $.ajax({ 
     url: "http://dev.site1.com/api/key", 
     crossDomain: true, 
     type: "PUT", 
     dataType: "jsonp", 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 
      alert(errorThrown); 
     }, 
     success: function(data){ 
      for (var i = keys.length - 1; i >= 0; i--) { 
       console.log(keys[i]); 
      }; 
     } 
    }); 
}); 

REST-SERVER:在key.php下application/controllers/api/key.php

應該涉及的key.php文件的摘錄https://github.com/philsturgeon/codeigniter-restserver

看看具體到這個過程:

/** 
* Key Create 
* 
* Insert a key into the database. 
* 
* @access public 
* @return void 
*/ 
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 
    } 
} 

響應/請求頭

Request URL:http://dev.mapitusa.com/api/key 
Request Method:PUT 
Status Code:403 Forbidden 
Request Headersview source 
Accept:application/json, text/javascript, */*; q=0.01 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:0 
Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22e165df34aa4fda5936e940658030f83d%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328291821%3B%7Dac0f163b112dbd3769e67f4bb7122db2 
Host:dev.mapitusa.com 
Origin:http://dev.mapitusa.com 
Referer:http://dev.mapitusa.com/api_test.html 
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.3 Safari/535.19 
Response Headersview source 
Cache-Control:max-age=0, public 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:69 
Content-Type:application/json 
Date:Fri, 03 Feb 2012 18:03:54 GMT 
Expires:Fri, 03 Feb 2012 18:03:54 GMT 
Keep-Alive:timeout=5, max=98 
Server:Apache 
Set-Cookie:ci_session=a%3A4%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22f2f466f7b97b89f2a9b557d2d9a0dbcc%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A9%3A%22127.0.0.1%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A118%3A%22Mozilla%2F5.0+%28Macintosh%3B+Intel+Mac+OS+X+10_7_3%29+AppleWebKit%2F535.19+%28KHTML%2C+like+Gecko%29+Chrome%2F18.0.1025.3+Safari%2F535.19%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1328292234%3B%7D6821b96c7e58b55f1767eb265ffdb79e; expires=Fri, 03-Feb-2012 20:03:54 GMT; path=/ 
Status:403 
Vary:Accept-Encoding,User-Agent 
X-Powered-By:PHP/5.3.6 
X-UA-Compatible:IE=Edge,chrome=1 

回答

3

我最終找到了403禁止是因爲我沒有提供API密鑰生成密鑰..

類abiguous菲爾的文檔是現有的API密鑰,然後才能要求並不說明生成密鑰..

我只是在數據庫表中創建一個僞造的鑰匙,並引用了調用/鍵/索引時?X-API-KEY = boguskey

+1

你可以在數據庫中插入一個,如果你不想要的話,你並沒有明確地使用api生成控制器。事實上,您可以在應用程序中創建隨機密鑰,然後將它們也放入數據庫中。它是通用的,你可以做你喜歡的;) – 2012-09-12 09:58:24

+0

我有同樣的問題,我生成一個關鍵,因爲you.mine不工作,幫助我 – 2015-06-10 07:20:40

0

如果從不同的域調用此,您可能會運行到一些XSS問題。您可能必須從您自己的服務器運行它,並從它自己的域中調用該函數,或者可能使用JSONP功能。

更新:您是否能夠使用NET選項卡查看Firebug中的事務? 你得到JSON嗎? 有時你必須添加回調=?到url請求: http://dev.site1.com/api/key?callback=

UPDATE2:你能帶來向上翻頁瀏覽器:(http://dev.mapitusa.com/api/key) 如果你得到同樣的錯誤,你應該嘗試給777(全讀/寫)權限。

+0

代碼更新,但它從我自己的域名.. – gorelative 2012-02-03 18:19:27

+0

與工作流/場景 – gorelative 2012-02-03 18:21:55

+0

加入響應/請求標頭以及質疑更多的解釋更新的問題.. – gorelative 2012-02-03 18:28:26

0

這聽起來像它可能是一個瀏覽器問題。也許在XMLHttpRequest棧中PUT的不正確實現。

我會盡快將其轉換爲POST以查看它是否有效。無論如何,只是爲了兼容性的目的,最好將它作爲POST。

2

我已經解決了生成API的問題鍵。 我正在使用Phil Sturgeon的REST API服務器。 呼叫使用Ajax調用這樣的關鍵控制:

$("#submitGetApiKey").click(function(){ 
    $.ajax({ 
     url: "http://sitename.com/api/key/index?X-API-KEY=your_key_here", 
     crossDomain: true, /* remove this if using the same domain*/ 
     type: "PUT", 
     dataType: "jsonp", 
     error: function(XMLHttpRequest, textStatus, errorThrown){ 
      alert(errorThrown); 
     }, 
     success: function(data){ 
      for (var i = keys.length - 1; i >= 0; i--) { 
       console.log(keys[i]); 
      }; 
     } 
    }); 
}); 

內鍵控制器: 搜索功能_generate_key()和檢查$這個 - >負載>幫手(「安全」);.必須加載安全幫助程序才能使用do_hash,否則您將收到500個內部服務器錯誤。

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 
    } 
} 

另外,你可以通過在你的鑰匙控制的微小變化 可以代替函數名index_put與index_get調用瀏覽器的地址欄中http://sitename.com/api/keyindex?X-API-KEY=your_key_here

感謝