How to pass KS token to the java service classes?

I’m trying to create a live stream entry for which I need to pass the Kaltura session.

I found a sample code in the below link (click on the Test console tab. And select the livestream service from the dropdown. click on Java and click show code example). And was trying to follow along. But the Kaltura version (18.0.1) I use doesn’t support this style.

Kaltura - Test Me Console

Maven dependency used:

<dependency>
  <groupId>com.kaltura</groupId>
  <artifactId>kalturaApiClient</artifactId>
  <version>18.0.1</version>
</dependency>

Is anyone aware of how to pass the Kaltura session to the service class in java? Appreciate your help!

Setting up the java configuration:

Configuration config = new Configuration();
config.setEndpoint(“https://www.kaltura.com/“);
Client client = new Client(config);

Generating the kaltura session using Java:

String session = client.generateSessionV2(“some admin secret", “some email id",
SessionType.ADMIN , partnerId, expiry, “some privileges”);

Trying to create a live stream entry using java:

LiveStreamEntry liveStreamEntry = new LiveStreamEntry();
liveStreamEntry.setName(“Webcast Tutorial”);
liveStreamEntry.setDescription(“This is a test webcast”);
liveStreamEntry.setMediaType(com.kaltura.client.enums.MediaType.LIVE_STREAM_FLASH);
liveStreamEntry.setDvrStatus(DVRStatus.ENABLED);
liveStreamEntry.setDvrWindow(60);
liveStreamEntry.setSourceType(SourceType.LIVE_STREAM);
liveStreamEntry.setAdminTags(“kms-webcast-event,vpaas-webcast”);
liveStreamEntry.setPushPublishEnabled(LivePublishStatus.DISABLED);
liveStreamEntry.setExplicitLive(Boolean.TRUE);
liveStreamEntry.setRecordStatus(RecordStatus.PER_SESSION);
LiveStreamService.AddLiveStreamBuilder result = LiveStreamService.add(liveStreamEntry, SourceType.LIVE_STREAM);

Here is the sample code from Python way of doing the same:
Generating ks token from python

def client_for_admin(username, privileges):
kaltura_config = KalturaConfiguration(“partner id")
kaltura_config.serviceUrl = “https://www.kaltura.com/
client = KalturaClient(kaltura_config)
ks = client.session.start(
“admin secret",
username,
KalturaSessionType.ADMIN,
“partner id",
“expiry",
privileges)
client.setKs(ks)
return client

client = ks.client_for_admin(config.admin_email, “”)

Generating live stream using python
live_stream_entry = KalturaLiveStreamEntry()
live_stream_entry.name = “Webcast Tutorial”
live_stream_entry.description = “This is a test webcast”
live_stream_entry.mediaType = KalturaMediaType.LIVE_STREAM_FLASH
live_stream_entry.dvrStatus = KalturaDVRStatus.ENABLED
live_stream_entry.dvrWindow = 60
live_stream_entry.sourceType = KalturaSourceType.LIVE_STREAM
live_stream_entry.adminTags = “kms-webcast-event,vpaas-webcast”
live_stream_entry.pushPublishEnabled = KalturaLivePublishStatus.DISABLED
live_stream_entry.explicitLive = KalturaNullableBoolean.TRUE_VALUE
live_stream_entry.recordStatus = KalturaRecordStatus.PER_SESSION
result = client.liveStream.add(live_stream_entry, KalturaSourceType.LIVE_STREAM); // livestream is called from the client which has the kaltura session.

Hi,

Basically you need to configure your KalturaConfiguration instance to use the KS.
client.setSessionId(ks);

Then we have the requests you want to make. I would advice you to have a look at this file on how to use the API services to send a request:
https://github.com/kaltura/developer-platform/blob/master/test/golden/with_setup/java.java

It seems nowadays we’re using Builder creational classes to build requests. Then we send them using this class:
import com.kaltura.client.APIOkRequestsExecutor;

I could be wrong here, but I made it work doing something simple like listing media with a certain name.

Hi @kp_vishnu ,

Kaltura - Test Me Console was deprecated quite a while ago.
Please use https://developer.kaltura.com/console instead.

It includes a code generator for all supported clients, including Java. For example, have a look at this action:
https://developer.kaltura.com/console/service/liveStream/action/add

To see the client initialisation code, check the Show setup checkbox.

As @pnts-se correctly wrote, once you’ve generated the KS, you need to assign it to the client object using:

client.setSessionId(ks);

Here’s a full example (as per the above):

    public static Client generateKalturaClient() {
        Configuration config = new Configuration();
        config.setEndpoint("https://www.kaltura.com/");
        Client client = new Client(config);
        try {
            String session = client.generateSessionV2(
                  "YOUR_KALTURA_SECRET",
                  "YOUR_USER_ID",
                  SessionType.ADMIN,
                  0,
                  86400, "");
            client.setSessionId(session);
        } catch (Exception e) {
            System.out.println("Failed to start Kaltura session");
            System.exit(1);
        }
        return client;
    }