Using same ks with different client objects

Hello,

I am coding in javascript on the client side.
I would like to know if we can use the same KS with different KalturaClient objects ?
Because when I call the api media.listAction() a first time, I get correctly the list of medias, however when I refresh the page and call media.listAction() a second time by using a new KalturaClient object, but using the same ks than the first time (I have saved the ks in the browser session storage), I get as a result a KS string.
I don’t understand why ?

Should I use always the same KalturaClient object with a specific KS string ? Then should I save the KalturaClient object in session storage as well.

Thank you in advance for your help !

Hello,

The Kaltura API is stateless and you should always pass the KS when making API requests that require authentication/authorization.

To generate a KS with the JS client, you need to call:

client.session.start(function(success, ks) {
...
},
secret,
userId,
type,
partnerId,
expiry,
privileges);
}

The ks var will then contain a string which is the KS [Kaltura Session] which you need to assign to the client using:

client.setKs(ks);

From there onwards, you can use this client with this KS to make subsequent calls, until that KS expires.

That said, you can use the same KS with different clients rather than generating a new one using session.start(). Of course, bear in mind a KS has an expiry field, when null is passed for the expiry param, expiry will be set to 86400 seconds, which is 24 hours.

My guess, without looking at your code, is that you forgot to call client.setKs(ks) for the second client, but it’s just a guess.

There is a step by step code tutorial about generating a KS and assigning it here:
https://developer.kaltura.com/recipes/authentication#/start

and you can toggle code in several languages, including JS.

Additional code samples are available here:
https://developer.kaltura.com/recipes

If you still have issues with your code, please post an actual code snippet so I can help you further.

Also, depending on your use case, you might find working with app tokens instead of directly generating KS’ to be more appropriate. You can learn about how to use app tokens here:
https://developer.kaltura.com/recipes/app_tokens#

Hello,

I use the AngularJS framework :

log_in_service is a service I created for managing the user authentication. I define in it functions to authenticate, get and set the users credentials and save them in the browser.

This variable will contain the list of media that we will get from the media.listAction() call :
$scope.list_of_videos = [];

This function sets the result returned by media.listAction() in the above variable :

            $scope.set_list_of_videos = function(results){
                        $scope.list_of_videos = results;
                        console.log('set_list_of_videos : We have now the list of videos !');
                    };

This function is a promise. It calls the media.listAction() API and returns the result got from the API call.

$scope.get_list_videos_promise = function(client){
            console.log('Entered get_list_videos_promise function')
            var deferred = $q.defer();                        

            var filter = new KalturaMediaEntryFilter();

            var pager = new KalturaFilterPager();

            console.log('get_list_videos_promise : media.listAction call');
            client.media.listAction(function(success, results) {
              if (!success || (results && results.code && results.message)) {
                console.log('Kaltura Error', success, results);
                deferred.reject();
              } else {
                  console.log('get_list_videos_promise : ks used = ' + log_in_service.get_ks_user());
                console.log('Kaltura Result for media.listAction', results);
                deferred.resolve(results);
              }
            }, filter, pager);

            return deferred.promise;
        };

In this IF-ELSE, I check whether or not the user is already registered.
If the user is not connected, I call first the user authentication form, save the username and ks in the browser when the authentication is succesful, afterwards I make the call for the media list.
Otherwise I call directly the media list API.
In both cases I create a new client and use the ks saved in the browser.

if(log_in_service.get_ks_user() === ''){
            console.log('the user is not logged in');
            log_in_service.log_in_modal().then(function(result){
                                            console.log('logging successful and credentials saved in browser!');
                                             log_in_service.saveInBrowser();
                                             var config = new KalturaConfiguration(2086441);
                                            config.serviceUrl = 'https://www.kaltura.com';
                                            var client = new KalturaClient(config);
                                            client.setKs(log_in_service.get_ks_user());

                                            $scope.get_list_videos_promise(client).then(function(results){
                                                                                $scope.set_list_of_videos(results);
                                                                            })
                                                                            .catch(function(error){
                                                                                console.log("Couldn't get the list of videos");
                                                                               });
                                        })
                                        .catch(function(error){
                                            console.log('The log in process has failed : ' + error);
                                           });
        }
        else{
            console.log('the user is already logged in')
            var config2 = new KalturaConfiguration(2086441);
            config2.serviceUrl = 'https://www.kaltura.com';
            var client2 = new KalturaClient(config);
            client2.setKs(log_in_service.get_ks_user());
            $scope.get_list_videos_promise(client2).then(function(results){
                                                $scope.set_list_of_videos(results);
                                            })
                                            .catch(function(error){
                                                console.log("Couldn't get the list of videos");
                                               });
        }

Here are 2 screenshots.
The first one corresponds to the first time when I load the page and user is not connected.
Then the user connects herself using the authentication form. The username and ks are saved in the browser.

The second one corresponds to when the user is already authenticated (ks saved in browser) and I refresh the page.

You can see on the second screenshot that the media.listAction() returns a string looking like a KS, and not the media list object as in the first screenshot.

Thanks for you help !

Can you please explain me more about the appToken concept.

I read that session.start is not the most secured way to get a KS, especially on the client side, because we make visible the SECRET code.

The best way is to, according the doc, use user.loginByLoginId (I use user.login). Isn’t it ?