如何使用 phonegap + quickblox 注册/登录用户?

Posted

技术标签:

【中文标题】如何使用 phonegap + quickblox 注册/登录用户?【英文标题】:How to sign up/login a user using phonegap + quickblox? 【发布时间】:2014-10-21 15:05:14 【问题描述】:

我一直在 phonegap 中开发一个应用程序,想要调用 quickblox 后端服务。

应用成功获取应用令牌并创建 API 会话,但 user.create 代码失败 - 这是我的 index.html 代码

    <!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title></title>
    <!--  -->
    <link rel="stylesheet" href="css/themes/.css" />
    <link rel="stylesheet" href="css/themes/jquery.mobile.icons.min.css" />
    <link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.3/jquery.mobile.structure-1.4.3.min.css" />
    <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.4.3/jquery.mobile-1.4.3.min.js"></script>
    <script src="js/config.js"></script>
    <script src="js/quickblox.js"></script>
</head>
<body>
    <div style="width:100%;">
        <h1 style="text-align:center;">The Activity Network</h1>
    </div>
    <div>
        <div data-role="tabs" id="tabs">
            <div data-role="navbar">
                <ul>
                    <li><a href="#one" data-ajax="false">Login</a></li>
                    <li><a href="#two" data-ajax="false">Register</a></li>
                </ul>
            </div>
            <div id="one">
                <h2>Login</h2>
                <input type="text" id="new_login" placeholder="Username" />
                <input type="password" id="new_password" placeholder="Password" />
                <input id="auth" type="submit" value="Submit" onclick="window.location='sportsSelector.html'" />
            </div>
            <div id="two">
                <h2>Register</h2>
                <input type="text" id="login" placeholder="Username"/>
                <input type="email" id="email" placeholder="E-Mail Address"/>
                <input type="email" id="confirmEmail" placeholder="Confirm E-Mail Address"/>
                <input type="password" id="password" placeholder="************"/>
                <input type="password" value="confirmPassword" placeholder="************"/>
                <input id="register" type="submit" value="Submit" />
            </div>
        </div>
     </div>
    <!-- Cordova reference, this is added to your app when it's built. -->
    <script src="cordova.js"></script>
    <script src="scripts/platformOverrides.js"></script>

    <script src="scripts/index.js"></script>
    <script>
        $(document).ready(function () 
            QB.init(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);
            //console.log(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);
            QB.createSession(function (err, result) 
                console.log(result);
                // callback function
            );
        );

        var username;
        var password;
        var email;

        $('#register').click(function () 
            console.log("Ready! Getting the user details.");

            username = document.getElementById('login').value;
            password = document.getElementById('password').value;
            email = document.getElementById('email').value;

            console.log("Username: ", username);
            console.log("Password: ", password);
            console.log("E-Mail: ", email);

            var params, connection;

            params = 
                login: username,
                password: password,
                email: email
            ;

            //console.log("Params: ", params);

            create(params);
        );

        function create(params) 
            console.log("Creating new user");
            QB.users.create(params, function (err, result) 
                console.log("Result: ", result);
                // callback function
            );
        
    </script>
</body>
</html>

如果有人能发现我缺少的东西,我会非常感激。目前,它在控制台中吐出以下内容:

    XHR finished loading: POST "http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=https%3A//users.quickblox.com/users.json".
[DEBUG] Add new user: POST https://users.quickblox.com/users.json?user[owner_id]=&user[login]=Username&email[email]=undefined&user[password]=Password&token=undefined index.html:197

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=https%3A//users.quickblox.com/users.json

【问题讨论】:

【参考方案1】:

我认为您的其他脚本之一(本地)可能会干扰。将您的代码复制到 jsfiddle,添加了一些虚拟凭据,它工作得很好。

$(document).ready(function () 
    var QBAPP = 
        app_id: replace_with_yours,
        auth_key: replace_with_yours,
        auth_secret: replace_with_yours
    ;

    QB.init(QBAPP.app_id, QBAPP.auth_key, QBAPP.auth_secret);

    QB.createSession(function (err, result) 
        console.log(result);
        // callback function
    );
);

var username;
var password;
var email;

$('#register').click(function () 
    console.log("Ready! Getting the user details.");

    username = document.getElementById('login').value;
    password = document.getElementById('password').value;
    email = document.getElementById('email').value;

    console.log("Username: ", username);
    console.log("Password: ", password);
    console.log("E-Mail: ", email);

    var params, connection;

    params = 
        login: username,
        password: password,
        email: email
    ;

    create(params);
);

function create(params) 
    console.log("Creating new user");
    QB.users.create(params, function (err, result) 
        console.log("Result: ", result);
        // callback function
    );

【讨论】:

谢谢!断开连接的是ripple.js波纹模拟器:D它在我的设备上完美运行。谢谢! 我有类似的问题,它在浏览器上运行良好,但 QB.users.create 仅在设备上失败【参考方案2】:

这是您的要求:

POST https://users.quickblox.com/users.json?user[owner_id]=1622883&user[login]=Username&email[email]=undefined&user[password]=Password&token=undefined

至少电子邮件和令牌有问题:

email[email]=undefined
token=undefined

尝试仔细检查这些参数

【讨论】:

以上是关于如何使用 phonegap + quickblox 注册/登录用户?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 quickblox 网页版

究竟如何在 android 中使用 quickblox?

如何使用 api 创建 quickblox 应用程序?

如何使用 quickblox 实时获取推送通知

QuickBlox - 如何使用 REST API + PHP 创建 blob 内容

如何在 React Native 中集成 QuickBlox?