2017  Kodetalk | Feedback | Privacy Policy | Terms | About
userimage

Quickblox facing problem to generate the token by using REST API

I am quite new to Quickblox and working on push notification implementation on my current application. But before push we need to get the token and activate the session first. And I am stuck here to get the token. What might be the problem please help me out from this.
userimage

If you follow the QuickBlox official sitehttp://quickblox.com/developers/Authentication_and_Authorization#With_User_authorization its too easy to get the session token. But for the new commerce little confusion too. I am pretty much sure your problem with generate the signature which may not be acceptable and no return for the same.


{"application_id": "2", "auth_key": "DtF9cZPqTF8Wy9Q", "timestamp": "1333630392", "nonce": "1236221330", "signature": "eb0ec2d8c8184a3e62b41da2afb6e8d690577fa4"}


Above one is the data format given in quickblox site. Just follow the process of signature generation then your prolem will resolve.

You need to give proper nonce and timestamp to generate the signature, with the format as, 'application_id=22&auth_key=wJHd4cQSxpQGWx5&nonce=33432&timestamp=1326966962'

And try again sure you will success.

userimage

Here you can find the code snippet to generate QuickBlox signature


import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Formatter;
import java.util.Random;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class QuickBloxGenerateSignatureUtil {
    static final String appId = "xxx";
    static final String authKey = "xxx";
    static final String authSecret = "xxx";

    public static String getSignatureDetails() {
        long timestamp = System.currentTimeMillis() / 1000L;
        int nonce = new Random().nextInt();

        String message = "application_id=appId&auth_key=authKey" + nonce + "&timestamp=" + timestamp;
        String secret = "xxx";

        String hmac = null;
        try {
            hmac = calculateRFC2104HMAC(message, secret);
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (SignatureException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return nonce + "," + timestamp + "," + hmac;
    }

    private static String calculateRFC2104HMAC(String data, String key) throws SignatureException,
            NoSuchAlgorithmException, InvalidKeyException {
        //HmacSHA1 is the name of an 'HMAC-SHA' Algorithm
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(signingKey);
        return toHexString(mac.doFinal(data.getBytes()));
    }
    
    private static String toHexString(byte[] bytes) {
        Formatter formatter = new Formatter();
        byte[] arrayOfByte = bytes;
        int j = bytes.length;
        for (int i = 0; i < j; i++) {
            byte b = arrayOfByte[i];
            formatter.format("%02x", new Object[] { Byte.valueOf(b) });
        }
        return formatter.toString();
    }
}

Answer is