DeveloperDeveloper
  • Standard Solutions
  • Cam Solutions
  • Native SDKs
  • App Gallery
  • Game Engine Plugins
Forum
Projects Library
Dev Discord
  • Standard Solutions
  • Cam Solutions
  • Native SDKs
  • App Gallery
  • Game Engine Plugins
Forum
Projects Library
Dev Discord
  • Cam Solutions (with Lovense Connect)

    • Introduction
    • Cam Extension for Chrome
    • Cam Kit for Web
    • Display Panel (optional)
    • Basic API
    • Basic Socket API
    • Basic JS SDK
    • Viewer JS
Got Stuck?
Forum for Lovense Developers
Support
Document Feedback

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:

NameDescriptionRequired
tokenDeveloper Tokenyes
modelUidModel ID on your applicationyes
customerUidCustomer ID on your applicationyes

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.

Control Panel

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
})
Last Updated:
Explore our Forum or Support to get more inspiration or solve your problems.
Discord Channel
It is an online real-time channel where you can communicate directly with our official administrators or many excellent developers.
Forum
It is a place for Lovense developers to communicate, where you can find solutions to problems or get inspired for your projects.
Support
Find documents and tutorials that may be helpful to you.