Search code examples
androidquickblox

How do I connect to the Quickblox backend by using "Getting Started" in the SDK documentation?


I've spent the better part of 2 days trying to understand and follow the "Getting Started" section in the Android SDK doc (I'm a noob, so please go slow). I've also been picking through Quickblox API documentation, Stack overflow Quickblox Q&A's, and the Quickblox sample code.

Could someone please explain how I can actually establish a very simple and basic session with the Quickblox backend?

In trying to do this myself, here's what's I've run into/discovered:

Under the section "Getting Started" (in http://quickblox.com/developers/Android) the following steps are outlined:

  1. Initialize framework with application credentials
  2. Create session
  3. Login with existing user or register new one
  4. Perform actions with QuickBlox communication services and any data entities (users, locations, files, custom objects, pushes etc.)

For #1 above, it gives the following code:

QBSettings.getInstance().fastConfigInit("961", "PBZxXW3WgGZtFZv", "vvHjRbVFF6mmeyJ");

I put the above in the OnCreate method of my activity.

Then, for #2, it says "To create an application session use this code:"

QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
    @Override
    public void onSuccess(QBSession session, Bundle params) {
        // success
    }

    @Override
    public void onError(List<String> errors) {
        // errors
    }
});

I also add that to my OnCreate.

For #3, I continue to use the suggested code. Is this case, I am creating a new user:

// Register new user
final QBUser user = new QBUser("userlogin", "userpassword");

QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
    @Override
    public void onSuccess(QBUser user, Bundle args) {
        // success
    }

    @Override
    public void onError(List<String> errors) {
       // error
    }
});

Here's my full OnCreate code:

public class ChatCategoryActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chat_category);

        Toast.makeText(getApplicationContext(), "Toast Test!",
                Toast.LENGTH_LONG).show();


        //Initialize quickblox framework
        QBSettings.getInstance().fastConfigInit("29430", "XNKu54nymZXFq3c", "3vy372mwtYwfJU7");

        //create a quickblox application session
        QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
            @Override
            public void onSuccess(QBSession session, Bundle params) {
                // success
                Toast.makeText(getApplicationContext(), "App session created!",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(List<String> errors) {
                // errors
                Toast.makeText(getApplicationContext(), "Failed to create app session!",
                        Toast.LENGTH_LONG).show();
            }
        });

        // Register new user
        final QBUser user = new QBUser("bob1", "bobobob1");

        QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
            @Override
            public void onSuccess(QBUser user, Bundle args) {
                // success
                Toast.makeText(getApplicationContext(), "User signed up!",
                        Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(List<String> errors) {
                // error
                Toast.makeText(getApplicationContext(), "User sign-up failed!",
                        Toast.LENGTH_LONG).show();
            }
        });
    }
}

So, when I run it, according to the toast that fires, the app session fails to be created. Additionally, the URL that the app posts is this:

https://api.quickblox.com/session.json?application_id=29430&auth_key=XNKu54nymZXFq3c&nonce=166079749&timestamp=1444750770&signature=a412ecb12db54842f6816968a734b4fc2626509d

And the response is:

{"errors":["Token is required"]}

The only place a "token" is mentioned in the Android SDK doc is here:

It's also possible to initialise the SDK with an existent QuickBlox token. It can be interesting in cases when you build a big system and you have the server side which generates QuickBlox tokens for example...

The implication is that the token is not necessary. But clearly it is.

Could someone please help me with what what I am missing? I'd be very grateful for the code I need, including how to generate the token (including the SHA signature) and use it to initialise the framework, create a session, create/login a user, etc....

Many thanks!


Solution

  • First check: internet connection, do you have the internet permission in your manifest?

    <uses-permission android:name="android.permission.INTERNET"/>
    

    Your code to create session and user looks fine but you can only sign up once the session is created !

    Using your code it would looks like that :

       public class ChatCategoryActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //Initialize quickblox framework
            QBSettings.getInstance().fastConfigInit("29430", "XNKu54nymZXFq3c", "3vy372mwtYwfJU7");
    
            //create a quickblox application session
            QBAuth.createSession(new QBEntityCallbackImpl<QBSession>() {
                @Override
                public void onSuccess(QBSession session, Bundle params) {
                    // success
                    Toast.makeText(getApplicationContext(), "App session created!",
                            Toast.LENGTH_LONG).show();
    
                    // Register new user
                    final QBUser user = new QBUser("bob1", "bobobob1");
    
                    QBUsers.signUp(user, new QBEntityCallbackImpl<QBUser>() {
                        @Override
                        public void onSuccess(QBUser user, Bundle args) {
                            // success
                            Toast.makeText(getApplicationContext(), "User signed up!",
                                    Toast.LENGTH_LONG).show();
                        }
    
                        @Override
                        public void onError(List<String> errors) {
                            // error
                            Toast.makeText(getApplicationContext(), "User sign-up failed!",
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
    
                @Override
                public void onError(List<String> errors) {
                    // errors
                    Toast.makeText(getApplicationContext(), "Failed to create app session!",
                            Toast.LENGTH_LONG).show();
                }
            });
    
    
        }
    }
    

    If the credentials are OK, a new user should be created. (for security reasons don't forget to reset the credentials and update your code.)