2013-01-01 4 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

당신은 자신의 응용 프로그램 매개 변수를 사용해야합니다 :

  • auth_key

랜덤 '넌스'와 현재의 타임 스탬프 (안 예에서,이에 현재의 타임 스탬프를 얻을 수 있습니다 APPLICATION_ID 사이트 http://www.unixtimestamp.com/index.php)

코드가 맞지만 적절한 매개 변수를 설정해야합니다.

+0

감사합니다! 내 application_id와 auth_key 및 생성 된 타임 스탬프와 난수를 사용했습니다. 귀하의 회신에 감사드립니다 – hitender

+0

! 내 application_id와 auth_key 및 생성 된 타임 스탬프와 난수를 사용했습니다. 타임 스탬프의 경우 strtotime (날짜 ("Y-m-d h : i : s")); strtotime (날짜 ('m/d/Y h : m : s')); 타임 스탬프를 생성하려면 timestamp를 생성하고 random number를 생성하는 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

설명서에 오류가 있습니다. 문서의 예제 웹 서비스에는 { "application_id": "2", "auth_key": "DtF9cZPqTF8Wy9Q", "timestamp": "1333630580", "nonce": "1340569516", "signature": "13293a5bd2026b957ebbb36c89d9649aae9e5503 ","user ": {"로그인 ":"injoit ","password ":"injoit "}}. 사용자 정보는 "사용자"요소에 있습니다. 올바른 방법은 "user"요소를 사용하지 않는 것입니다. – 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); 
관련 문제