2013-01-01 17 views
5

我想訪問QuickBlox中的API,但在此之前,我們需要驗證我們的應用程序並獲取會話令牌,並使用會話令牌訪問其他API。 但問題是,當我發送上使用QuickBloxwebsite給定所要求的規範的認證請求時,我收到錯誤消息:如何在PHP中生成QuickBlox認證簽名?

{「錯誤」:{「基地」:[「意外的簽名」] }}

生成簽名的參數是:

application_id=22&auth_key=wJHd4cQSxpQGWx5&nonce=33432&timestamp=1326966962 

然後我們把它轉換成HMAC-SHA格式:

hash_hmac('sha1', $signatureStr , $authSecret); 

請幫我解決這個問題。

回答

0

你必須使用自己的應用程序參數:

  • APPLICATION_ID
  • AUTH_KEY

和隨機「隨機數」和當前時間戳(不是從例如,你可以在這個當前的時間戳網站http://www.unixtimestamp.com/index.php

你的代碼是正確的,但你必須設置合適的參數

+0

感謝您的回覆!我使用我的application_id和auth_key以及我生成的時間戳和隨機數。 – hitender

+0

感謝您的回覆!我使用我的application_id和auth_key以及我生成的時間戳和隨機數。對於時間戳,我試試strtotime(date(「Y-m-d h:i:s」));和strtotime(日期('m/d/Y h:m:s'));爲了生成時間戳,我還嘗試使用time()函數來生成時間戳和隨機數。我使用rand()函數來獲得唯一的隨機號。在PHP中。但仍然給出同樣的錯誤。 – hitender

+0

我認爲問題在於如何獲得時間戳。 你可以嘗試從網站http://www.unixtimestamp.com/index.php 得到它,並用這個值運行你的代碼 –

2

我在php上寫了代碼片段,它會生成簽名。它的工作原理好

這是我的測試應用程序的憑據:

$application_id = 92; 
$auth_key = "wJHdOcQSxXQGWx5"; 
$authSecret = "BTFsj7Rtt27DAmT"; 

$nonce = rand(); 
echo "<br>nonce: " . $nonce; 

$timestamp = time(); 
echo "<br>timestamp: " . $timestamp ."<br>"; 

$stringForSignature = "application_id=".$application_id."&auth_key=".$auth_key."&nonce=".$nonce."&timestamp=".$timestamp; 
echo $stringForSignature."<br>"; 

$signature = hash_hmac('sha1', $stringForSignature , $authSecret); 
echo $signature; 

希望這有助於

2

問題解決

有在我的請求參數有問題。

$params = "application_id=$application_id&auth_key=$auth_key&timestamp=$timestamp&nonce=$nonce&signature=$signature&**auth_secret=$authSecret**"; 

在這個參數我經過一個額外的參數,my auth secret key它不應該存在。我刪除了這個參數,現在它的工作。

2

下面是完整的例子如何創建QuickBlox會話:

<?php 
// Application credentials 
DEFINE('APPLICATION_ID', 92); 
DEFINE('AUTH_KEY', "wJHdOcQSxXQGWx5"); 
DEFINE('AUTH_SECRET', "BTFsj7Rtt27DAmT"); 

// User credentials 
DEFINE('USER_LOGIN', "emma"); 
DEFINE('USER_PASSWORD', "emma"); 

// Quickblox endpoints 
DEFINE('QB_API_ENDPOINT', "https://api.quickblox.com"); 
DEFINE('QB_PATH_SESSION', "session.json"); 

// Generate signature 
$nonce = rand(); 
$timestamp = time(); // time() method must return current timestamp in UTC but seems like hi is return timestamp in current time zone 
$signature_string = "application_id=".APPLICATION_ID."&auth_key=".AUTH_KEY."&nonce=".$nonce."&timestamp=".$timestamp."&user[login]=".USER_LOGIN."&user[password]=".USER_PASSWORD; 

echo "stringForSignature: " . $signature_string . "<br><br>"; 
$signature = hash_hmac('sha1', $signature_string , AUTH_SECRET); 

// Build post body 
$post_body = http_build_query(array(
       'application_id' => APPLICATION_ID, 
       'auth_key' => AUTH_KEY, 
       'timestamp' => $timestamp, 
       'nonce' => $nonce, 
       'signature' => $signature, 
       'user[login]' => USER_LOGIN, 
       'user[password]' => USER_PASSWORD 
       )); 

// $post_body = "application_id=" . APPLICATION_ID . "&auth_key=" . AUTH_KEY . "&timestamp=" . $timestamp . "&nonce=" . $nonce . "&signature=" . $signature . "&user[login]=" . USER_LOGIN . "&user[password]=" . USER_PASSWORD; 

echo "postBody: " . $post_body . "<br><br>"; 
// Configure cURL 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, QB_API_ENDPOINT . '/' . QB_PATH_SESSION); // Full path is - https://api.quickblox.com/session.json 
curl_setopt($curl, CURLOPT_POST, true); // Use POST 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body); // Setup post body 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Receive server response 

// Execute request and read responce 
$responce = curl_exec($curl); 

// Check errors 
if ($responce) { 
     echo $responce . "\n"; 
} else { 
     $error = curl_error($curl). '(' .curl_errno($curl). ')'; 
     echo $error . "\n"; 
} 

// Close connection 
curl_close($curl); 
?> 
+0

我收到「意外簽名」錯誤的請求,請參閱我的問題:http://stackoverflow.com/questions/31733629/quickblox-rest-api-unexpected-signature-on-laravel – Maykonn

+0

文檔有錯誤。文檔中的示例Web服務具有以下主體:{「application_id」:「2」,「auth_key」:「DtF9cZPqTF8Wy9Q」,「timestamp」:「1333630580」,「nonce」:「1340569516」,「簽名」:「13293a5bd2026b957ebbb36c89d9649aae9e5503 「,」user「:{」login「:」injoit「,」password「:」injoit「}}。用戶信息位於「用戶」元素中。正確的方法不使用「用戶」元素。 – DragonT

0

我們使用PHP和下一個代碼很適合我們:

<?php 

$userLogin = '{YOUR_QB_USER}'; 
$userPassword = '{YOUR_QB_PASSWORD}'; 

$body = [ 
    'application_id' => '{YOUR_QB_APPLICATION_ID}', 
    'auth_key' => '{YOUR_QB_AUTH_KEY}', 
    'nonce' => time(), 
    'timestamp' => time(), 
    'user' => ['login' => $userLogin, 'password' => $userPassword] 
]; 
$built_query = urldecode(http_build_query($body)); 
$signature = hash_hmac('sha1', $built_query , '{YOUR_QB_APP_SECRET}'); 
$body['signature'] = $signature; 
$post_body = http_build_query($body); 
$curl = curl_init(); 
curl_setopt($curl, CURLOPT_URL, 'https://{YOUR_QB_HOST}/session.json'); 
curl_setopt($curl, CURLOPT_POST, true); 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_body); 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($curl); 
$token = json_decode($response, true)['session']['token']; 
printf('Your token is: %s %s', $token, PHP_EOL);