Detecting user during query custom API function

I can’t understand one thing:
For example, custom api function:

public String getExampleUserId(){


if (Backendless.UserService.isValidLogin()) {


return Backendless.UserService.loggedInUser();


} else {


return null;


}


}

In this example I always obtain null. Before this query user logged in with google with stayLoggedIn=true. How correctly get user/userId during any query (something like ownerId for new objects).

It is described in the doc:

https://backendless.com/documentation/users/android/users_login.htm

Specifically, right here:
http://support.backendless.com/public/attachments/40a9056cd22fa835b5dfa97439b85e34.jpg</img>

Yes, but isValidLogin always returns false. With what it can be connected?

Do you use the stayLogged argument in the “login” API call?

Yes, of course. I wrote about it in first comment.

May be this not clear: I want to get user id inside server side custom api method.

Working crutch in business logic method:

MyObject fakeObject = new MyObject();
fakeObject = fakeObject.save();
userId = fakeObject.getOwnerId();
fakeObject.remove();

How to avoid saving object to getting userId?

Hi, Handy.

Business logic has own context when invoke you code (InvocationContext class).
I’ve tried to reproduce your problem and have got the same behavior.
As workaround, try this in you code on the business logic side (at the begining of the method):



  
  
  
  

UserTokenStorageFactory.instance().getStorage().set( InvocationContext.getUserToken() );

Thank you very much. It’s work, but id also need get from InvocationContext:

UserTokenStorageFactory.instance().getStorage().set(InvocationContext.getUserToken());
if (Backendless.UserService.isValidLogin()) {
    String userId = InvocationContext.getUserId();
...

InvocationContext is the partial copy of request context from server side.
In HostedServices you should use InvocationContext, in EventHandlers - RunnerContext.

Thank you again. It’s very useful information. Too bad that I could not find documentation on this issue before.

In the next release we add automatic setting userId and userToken, and you can remove lines of code mentioned above.

Ok)