Authenticate

The authenticate function plays a crucial role in initializing the SDK. It establishes a secure session and prepares the SDK for subsequent operations. For optimal effectiveness, follow these guidelines before invoking the function:

Authentication Integration Guide

Introduction

Ensure the core module is integrated correctly for successful authentication.

Steps

  1. Verify Module Installation

  2. Configure Settings

    • Update configuration files with necessary credentials.

Generating a Signature

To enhance the security of your API requests, it's necessary to generate a signature if you have an API key and a Secret key. This process ensures that your requests are authenticated and can be trusted by the server. Follow these steps to generate a signature:

  1. Collect Necessary Keys:

    • Ensure you have your API key and Secret key ready. These are provided when you register for access to the API.

  2. Create the Signature String:

    • Combine specific parameters from your request into a single string. This usually involves the HTTP method, URL, and any query parameters or headers that are part of the request.

  3. Apply Hash Function:

    • Use a cryptographic hash function like HMAC with the Secret key to hash the signature string. The specific algorithm (e.g., SHA256) will depend on the API requirements.

  4. Encode the Signature:

    • The resulting hash should be encoded in base64 to form the final signature.

  5. Attach the Signature to Your Request:

    • Include the generated signature in the request headers or query parameters, as stipulated by the API documentation.

By following these steps, you can ensure that each request you make is securely signed and authenticated. Always refer to the specific API documentation for detailed implementation guidelines.

public String toSha256String(String key, String msg) {
   try {
  // getting instance of Message Authentication Code
       Mac hmacSha256 = Mac.getInstance("HmacSHA256");
//secretKey
       SecretKeySpec secretKey = new SecretKeySpec(key.getBytes(), "HmacSHA256");
       hmacSha256.init(secretKey);
       byte[] macData = hmacSha256.doFinal(msg.getBytes());

       // Convert byte array to hex string
       StringBuilder hexString = new StringBuilder();
       for (byte b : macData) {
           String hex = Integer.toHexString(0xff & b);
           if (hex.length() == 1) {
               hexString.append('0');
           }
           hexString.append(hex);
       }
       return hexString.toString();
   } catch (NoSuchAlgorithmException | InvalidKeyException e) {
       e.printStackTrace();
       return null;
   }
}
  1. Test Connection

  • Run tests to confirm successful integration.

public static void authenticate(){
//        TruvideoSdkCamera.getInstance().initCameraScreen(context);
    if (!isAuthenticated() || isAuthenticationExpired()) {
        // Get API key and secret key
        String apiKey = "YOUR-API-KEY"
        // generate payload for authentication
        String payload = TruvideoSdk.getInstance().generatePayload();
        String secretKey = "YOUR-SECRET-KEY"
        // generate SHA-256 hash of payload with signature as secret key
        String signature = toSha256String(secretKey,payload);
        String externalId = ""; // empty string if you don't have multitenant enabled, or the value for your sub-account
        // Authenticate user
        TruvideoSdk.getInstance().authenticate(apiKey, payload, signature,externalId,
            new TruvideoSdkCallback<>(){
                @Override
                public void onComplete(Unit unit) {
                    initialize();
                }
                @Override
                public void onError(@NonNull TruvideoSdkException e) {
                    // handle error
                }
        });
    }
    initialize();
    
}
public static void initialize() {
    TruvideoSdk.getInstance().initAuthentication(new TruvideoSdkCallback<>() {
        @Override
        public void onComplete(Unit unit) {
            // Authentication ready
        }

        @Override
        public void onError(@NonNull TruvideoSdkException e) {
            // handle error

        }
    });
}

Final Steps: Test Your Integration

After implementing the authentication:

  • Run your application to verify that the SDK session initializes successfully.

  • Monitor the logs for any issues related to signatures or tokens.

  • Once confirmed, proceed to explore media capture, uploading, and editing features.


Thank you for using the Truvideo SDK. You're now authenticated and ready to explore all features!

Last updated

Was this helpful?