4. Initialization

4.1. Verify the signature of initial data

When user logged in to the platform, the platform will redirect the user to the Login callback URL with the login information appended to the query string. Game server should verify these information before accepting the user's request.

Signature verification must be performed on the server, because app_secret must not be exposed to the client.

4.1.1. HTTP method

GET
  • Passed in URL query parameters
parameter name type description
login_key int64 random number acted as salt, provided by the platform
app_id string(60) the game id provided by the platform (not a constant value)
user_id int64 the user id provided by the platform
sign string(60) signature for validating the data

Keep in mind that app_id is not a fixed value, so avoid comparing it to a constant.

4.1.2. Example

https://your.game.host/?login_key=1000000&app_id=WAKOOL-APPID-TEST001&user_id=100000001&sign=4fcba2c21852edb8e6e3a1bc011aff66

The https://your.game.host/ in the above example refers to the Login callback URL game developers provided.

4.1.3. Signature verification

The signature is a lower-cased hexadecimal string of the MD5 value of a query string composed of the app_secret,login_key, app_id, and user_id in the specified order. The values of the query string have to be urlencoded first.

4.1.4. PHP example:

$sign_data = [
  'app_secret' => 'WAKOOL-APPSECRET-TEST001',
  'login_key' => '1000000',
  'app_id' => 'WAKOOL-APPID-TEST001',
  'user_id' => '100000001'
];

/**
 * constructing signature:
 */
$sign = md5(http_build_query($sign_data));

// The sign value above is 4fcba2c21852edb8e6e3a1bc011aff66

The 4 parameters have to be in the order demonstrated in the example code above when constructing the query string and the values have to be urlencoded.

If you have difficulties verifying the signature, check to see if the order of the four parameters are correct.

4.1.5. Java example:

String app_secret = "WAKOOL-APPSECRET-TEST001";
String login_key = "1000000";
String app_id = "WAKOOL-APPID-TEST001";
String user_id = "100000001";

/**
 * constructing signature:
 */
StringBuilder sb = new StringBuilder();
String cs = "UTF-8";
sb.append(java.net.URLEncoder.encode("app_secret", cs)).append('=')
  .append(java.net.URLEncoder.encode(app_secret, cs)).append('&')
  .append(java.net.URLEncoder.encode("login_key", cs)).append('=')
  .append(java.net.URLEncoder.encode(login_key, cs)).append('&')
  .append(java.net.URLEncoder.encode("app_id", cs)).append('=')
  .append(java.net.URLEncoder.encode(app_id, cs)).append('&')
  .append(java.net.URLEncoder.encode("user_id", cs)).append('=')
  .append(java.net.URLEncoder.encode(user_id, cs));

String sign = org.apache.commons.codec.digest.DigestUtils.md5Hex(sb.toString());

4.1.6. Python example:

try:
    from urllib.parse import urlencode
except ImportError:
    from urllib import urlencode
import hashlib

app_secret = 'WAKOOL-APPSECRET-TEST001'
login_key = '1000000'
app_id = 'WAKOOL-APPID-TEST001'
user_id = '100000001'

# constructing signature:
query = urlencode([('app_secret', app_secret), ('login_key', login_key), ('app_id', app_id), ('user_id', user_id)])
m = hashlib.md5()
m.update(query.encode('utf-8'))
sign = m.hexdigest()

4.1.7. Node.js example:

const sign_data = {
    app_secret: 'WAKOOL-APPSECRET-TEST001',
    login_key: '1000000',
    app_id: 'WAKOOL-APPID-TEST001',
    user_id: '100000001',
};

/**
 * constructing signature:
 */

const sign_string = new URLSearchParams(sign_data).toString();
const crypto = require('crypto')
const sign = crypto.createHash('md5').update(sign_string).digest('hex');

4.2. Importing SDK

The SDK is written in Javascript, simply insert the following code into your game page.

<script>
 !function(w,a,k,o,l,j,s){if(a.getElementById(o))return;j=function(x,f,c){
 c=decodeURIComponent;f=RegExp('[?&]'+x+'=([^&#]*)').exec(w.location.search);
 return f&&c(f[1].replace(/\+/g,' '))};s='app_id';l=l||j('wakool_'+s)||j(s);
 j=a.createElement(k);j.src='//api.wakool.net/sdk?'+s+'='+l+'&v='+Date.now();
 j.id=o;s=a.getElementsByTagName(k)[0];s.parentNode.insertBefore(j,s)}
 (window,document,'script','wakool-jssdk')
</script>

The SDK initialization depends on the 4 query parameters mentioned above: login_key, app_id, user_id, and sign. Please ensure that the necessary query parameters are present in the URL if it isn't the Login callback URL.

Beware that the 4 parameters DO NOT include app_secret!!!


4.3. SDK loaded callback

Create a function named wakoolAsyncInit that will be invoked by the SDK when DOMContentLoaded event is fired. The function should accept the SDK instance wakool as its parameter.

Within this function, game developers may register event listeners in order to be notified when platform events happened.

See Events for a complete list of events.

Games with in-app purchases are advised to register listeners for purchase event. Games that shows ads should listen for pause/resume event

In case the game is serverless, Login function is called here to verify user identity from the client side.

4.3.1. Example

window.wakoolAsyncInit = async function(wakool) {

  // Register SDK event listeners...

  wakool.addEventListener(wakool.EVENT_BROWSER_PURCHASE_VERIFY_VALID, function(event){
    //   …Purchase success…
    console.log('Purchase success! Wakool Order ID:', event.data.order_id);
  });

  // Call wakool.login() function here to obtain user info.
  const userInfo = await wakool.login();
  if (userInfo) {
    console.log('Login success! Player info:', userInfo);
  }

  // Store the SDK instance <wakool> to the window object for future use.
  window.wakool = wakool;
}

4.4. Obtain user info

After the SDK is loaded, call wakool.login() to retrieve user information, including the user id, nickname, and avatar.

Not all users has nickname or avatar.

wakool.login() is executed asynchronously and return a Promise object.

Call wakool.login() in the SDK loaded callback:

const userInfo = await wakool.login();
if (userInfo) {
 console.log('Login success! Player info:', userInfo);
} else {
 console.log('Login failed!');
}

When the login is successful, wakool.login() returns a JSON object contains the user information:

property name description
id The id of the user in Wakool platform.
nickname The nickname of the user.
avatar The avatar picture URL of the user.

results matching ""

    No results matching ""