Toggle navigation
Wialon Playground
Add library
jQuery latest
Bootstrap 3.3.1
Wialon Javascript SDK
Run
Get code
Fork
Save
Examples
Login
Get units
Change unit icon
Get messages
Get sensors
Edit sensors
Commands
Monitoring notification
Track layer
Get resources
Resources and accounts
Account parameters
Do payment
Management Driver
Create notification
Get geofences
Geofence parameters
Create geofence
Gurtam map
Units on map
Unit trace
Create report template
Execute report
Execute custom report
Create driver
Bind driver to unit
Unit edit fields
Import fillings
Token login for site
Advanced authorization form
Token usage in app
Account hierarchy
Nearest units
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>How to get token</title> <script type="text/javascript" src="https://hst-api.wialon.com/wsdk/script/wialon.js"></script> </head> <body> <!-- Call getToken() onclick --> <input id="login" type="button" value="Click to open login page" onclick="getToken()"/> <!-- Print token here--> Your token: <span id="token"></span> <br /><br /> <input id="logout" type="button" value="Click to logout" disabled onclick="logout()"/> <iframe id="login_iframe" style="width: 430px; height: 360px; border: 1px solid red;"></iframe> </body> </html>
HTML
#token { font-weight: bolder; }
CSS
// Wialon site dns var dns = "http://hosting.wialon.com"; // Main function function getToken() { // construct login page URL var url = dns + "/login.html"; // your site DNS + "/login.html" url += "?client_id=" + "App"; // your application name url += "&access_type=" + 0x100; // access level, 0x100 = "Online tracking only" url += "&activation_time=" + 0; // activation time, 0 = immediately; you can pass any UNIX time value url += "&duration=" + 604800; // duration, 604800 = one week in seconds url += "&flags=" + 0x1; // options, 0x1 = add username in response url += "&response_type=" + "hash"; url += "&redirect_uri=" + dns + "/post_token.html"; // if login succeed - redirect to this page // listen message with token from login page window window.addEventListener("message", tokenRecieved); // finally, open login page in new window //window.open(url, "_blank", "width=760, height=500, top=300, left=500"); var iframe_el = document.getElementById("login_iframe"); if (iframe_el) iframe_el.src = url; } // Help function function tokenRecieved(e) { // get message from login window var msg = e.data; if (typeof msg == "string" && msg.indexOf("access_hash=") >= 0) { // get token var token = msg.replace("access_hash=", ""); // now we can use token, e.g show it on page document.getElementById("token").innerHTML = token; document.getElementById("login").setAttribute("disabled", ""); document.getElementById("logout").removeAttribute("disabled"); // or login to wialon using our token wialon.core.Session.getInstance().initSession(dns); wialon.core.Session.getInstance().loginAuthHash(token, "", function(code) { if (code) return; var user = wialon.core.Session.getInstance().getCurrUser().getName(); alert("Authorized as " + user); }); // remove "message" event listener window.removeEventListener("message", tokenRecieved); } } function logout() { var sess = wialon.core.Session.getInstance(); if (sess && sess.getId()) { sess.logout(function() { document.getElementById("logout").setAttribute("disabled", ""); document.getElementById("login").removeAttribute("disabled"); }); } }
JS
Result
Source code of example
Close ✕
×
Source code