Viewer JS
Application for user authentication token
Use your developer token to apply for an authentication token for your users. You can get the developer token from developer dashboard.
WARNING
For security reasons, the developer token should always be used on the server side. You should never use it in your JS code from the client side.
API URL: https://api.lovense-api.com/api/basicApi/customer/getToken
Request Protocol: HTTPS Request
Method: POST
Request Content Type: application/json
Parameters:
Name | Description | Required |
---|---|---|
token | Developer Token | yes |
modelUid | Model ID on your application | yes |
customerUid | Customer ID on your application | yes |
Example:
curl -X POST "https://api.lovense-api.com/api/basicApi/customer/getToken" \
-H "Content-Type: application/json" \
-d '{
"token": "{Lovense developer token}",
"modelUid": "{model ID on your application}",
"customerUid": "{user ID on your application}"
}'
String url= "https://api.lovense-api.com/api/basicApi/customer/getToken";
Map<String, String> requestParameter = new HashMap<String, String>();
//TODO initialize your parameters:
requestParameter.put("token", "{Lovense developer token}");
requestParameter.put("modelUid", "{model ID on your application}");
requestParameter.put("customerUid", "{user ID on your application}");
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
if (requestParameter != null && !requestParameter.isEmpty()) {
Set<String> keys = requestParameter.keySet();
for (String key : keys) {
nameValuePairs.add(new BasicNameValuePair(key, requestParameter.get(key)));
}
}
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));
import axios from "axios"
const result = await axios.post(
"https://api.lovense-api.com/api/basicApi/customer/getToken",
{
token: "{Lovense developer token}",
modelUid: "{model ID on your application}",
customerUid: "{user ID on your application}",
}
)
Result:
{
code: 0
message: "Success"
data: {
"authToken": "{autoToken}"
}
}
Import the Javascript file
Import the Javascript file to your web page. This Javascript will declare a global variable LovenseViewerSdk
on the page.
<script src="https://api.lovense-api.com/viewer-sdk/core.min.js"></script>
TIP
Please add the following domains to your CSP whitelist.
*.lovense.com *.lovense-api.com *.lovense.club:*
Initialize
Declare an instance object using the LovenseViewerSdk
constructor. The ready
event is triggered after successful declaration.
WARNING
The authToken
is the user authorization token obtained in the first step.
/**
* @param {object} option
* @param {string} option.platform this is the Website Name shown in the developer dashboard
* @param {string} option.authToken authorization token, please attach this token isn't the Developer Token
* @returns {object} instance object
*/
LovenseViewerSdk(option)
Example:
const viewerJsInstance = new window.LovenseViewerSdk({
platform: "{platform}",
authToken: "{authToken}"
})
viewerJsInstance.on("ready", () => {
console.log("ready")
})
viewerJsInstance.on("sdkError", err => {
console.log('>>>>>> error', err.code, err.message)
})
Start toy control
/**
* @param {object} option
* @param {string} option.target Encrypt using AES with model ID and timestamp.
*/
startControl(option)
The parameter target
is a JSON string composed of the model ID and timestamp, encrypted using AES.
The format of the JSON string before encryption is:
{
uid: '{modelUid}',
time: {timestamp}
}
You will get AES KEY and AES IV that are used for encryption.
Example:
let payload = {
uid: '{modelUid}',
time: +new Date()
}
let target = jsEncrypt(JSON.stringify(payload), '{key}', '{iv}')
viewerJsInstance.startControl({ target })
Here is the demo on how to encrypt text using AES in JAVA:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.AlgorithmParameters;
import java.security.Key;
public class AESDemo {
private static Key getKey(String key) throws Exception{
byte[] keyBytes = key.getBytes("UTF-8");
SecretKeySpec newKey = new SecretKeySpec(keyBytes, "AES");
return newKey;
}
private static AlgorithmParameters getIV(String iv) throws Exception {
byte[] ivs = iv.getBytes("UTF-8");
AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
params.init(new IvParameterSpec(ivs));
return params;
}
public static String encrypt(String key,String iv,String text) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKey(key), getIV(iv));
byte[] encryptedBytes = cipher.doFinal(text.getBytes());
return new String(Base64.encodeBase64(encryptedBytes, false,true),"UTF-8");
}
public static String decrypt(String key,String iv,String text) throws Exception {
byte[] textBytes = Base64.decodeBase64(text);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(key), getIV(iv));
byte[] decodedBytes = cipher.doFinal(textBytes);
return new String(decodedBytes, "UTF-8");
}
public static void main(String[] args) throws Exception{
String key="jHZZwiizsAF2qTAY"; //use your own Key here
String iv="zijknVpNWeeTYGPV"; //use your own IV here
String test="Hello World!";
System.out.println(encrypt(key,iv,test));;
System.out.println(decrypt(key,iv,encrypt(key,iv,test)));;
}
}
End control
endControl()
Example:
viewerJsInstance.endControl()
Event listener
toyControlStart
Call this method after starting control
viewerJsInstance.on('toyControlStart', () => {
// todo
})
toyControlEnd
Call this method after ending control
viewerJsInstance.on('toyControlEnd', () => {
// todo
})