如何在 PHP 中生成 QuickBlox 身份验证签名?

Posted

技术标签:

【中文标题】如何在 PHP 中生成 QuickBlox 身份验证签名?【英文标题】:How to generate QuickBlox authentication signature in PHP? 【发布时间】:2013-01-01 08:23:43 【问题描述】:

我想访问 QuickBlox 中的 API,但在此之前我们需要对我们的应用程序进行身份验证并获取会话令牌,并且使用会话令牌我们可以访问其他 API。 但问题是,当我使用 QuickBlox 网站上提供的所需规范发送身份验证请求时,我收到错误消息:

"errors":"base":["意外签名"]

生成签名的参数为:

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

然后我们将其转换为 HMAC-SHA 格式:

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

请帮我解决这个问题。

【问题讨论】:

【参考方案1】:

我在 php 上写了代码 sn-p,它会生成签名。效果不错

这是我的测试应用程序的凭据:

$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,它不应该存在。我删除了这个参数,现在它可以工作了。

【讨论】:

【参考方案3】:

这里是如何创建 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);
?>

【讨论】:

我收到“意外签名”错误,请参阅我的问题:***.com/questions/31733629/… 文档有错误。文档中的示例 web 服务具有以下正文: "application_id": "2", "auth_key": "DtF9cZPqTF8Wy9Q", "timestamp": "1333630580", "nonce": "1340569516", "signature": "13293a5bd2026b957ebbb36c89d9649aae9e50 ”,“用户”:“登录”:“injoit”,“密码”:“injoit”。用户信息位于“用户”元素中。正确的方式不要使用“用户”元素。【参考方案4】:

您必须使用自己的应用程序参数:

application_id auth_key

以及随机的“nonce”和当前时间戳(不是来自示例,您可以在此站点上获取当前时间戳http://www.unixtimestamp.com/index.php)

你的代码是对的,但你必须设置正确的参数

【讨论】:

感谢您的回复!我已经使用了我的 application_id 和 auth_key 以及我生成的时间戳和随机数。 感谢您的回复!我已经使用了我的 application_id 和 auth_key 以及我生成的时间戳和随机数。对于时间戳,我尝试 strtotime(date("Y-m-d h:i:s"));和 strtotime(date('m/d/Y h:m:s'));为了生成时间戳,我还尝试了 time() 函数来生成时间戳和随机数。我使用 rand() 函数来获取唯一的随机数。在 php.ini 中。但仍然给出同样的错误。 我认为问题在于如何获得时间戳。您能否尝试从站点 unixtimestamp.com/index.php 获取它并使用此值运行您的代码 我也尝试了给定链接中的时间戳,但仍然无法正常工作。我在这里给出我的请求页面链接,我在其中显示我的服务器时间戳和随机数。 pl。看看。或者如果您需要任何其他信息,请。让我知道。 demo.allwikan.com/test-chat/qb-auth.php 【参考方案5】:

1) 您应该向正确的 url 发送请求。

到 https://api.quickblox.com/auth.json

改为 https://api.quickblox.com/session.json

2) 你应该使用它来修复 SSL 问题。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false)

【讨论】:

【参考方案6】:

我们使用 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);

【讨论】:

以上是关于如何在 PHP 中生成 QuickBlox 身份验证签名?的主要内容,如果未能解决你的问题,请参考以下文章

OAuth 1.0 在 POSTMAN 和 PHP 代码中生成签名差异

如何在 python 中生成唯一的身份验证令牌?

锂PHP框架如何在PHP页面中生成$this上下文?

如何在 PHP 中生成 300X200 尺寸的图像缩略图?

如何在php中生成自定义图像?

如何在 PHP 中生成字符串的所有排列?